23

My application's need is as follows: I upload the image to Cloudinary and store the url of image in my mongodb database.

To upload the image to cloudinary, I needed to give the file path, and for that, I am using multer.I use the statement:

app.use(multer({ dest: './uploads/'}));

The problem I face is that, everytime I upload an image from my local system to the database on Cloudinary, a local copy gets created in './uploads/'.I want this to not happen, since my images are there on Cloudinary.

I read the documentation of multer where it says:

Multer accepts an options object, the most basic of which is the dest property, which tells Multer where to upload the files. In case you omit the options object, the file will be renamed and uploaded to the temporary directory of the system.

I am unable to make out if that temporary upload space needs cleaning or if it is taken care of.My images are uploaded on Cloudinary.I used multer only for getting :

req.files.photo.path

to work.Is there any other way to do the same or if multer can be configured in a way to not store images to my local system?

Aravind
  • 3,169
  • 3
  • 23
  • 37
  • Did you got your question resolved? I am having this same issue. Their doc says use memory storage as a storage option instead of disk. I used it but it does not work. – codeinprogress Apr 04 '16 at 12:43
  • @codeinprogress: Yes, the answer below didn't work for me too, that is why I have not accepted it. My requirement in the project had changed, and I was okay for the memory this was taking. Do post an answer if you find a way to work around this. – Aravind Apr 05 '16 at 06:00
  • Have to ask the Cloudinary guys directly. – codeinprogress Apr 05 '16 at 08:29
  • This is not specific to Cloudinary. @codeinprogress: Did you try Ruslan's answer below? If that does not work, you can raise a pull request in multer's repo, or in busboy's repo on Github asking your doubt. Do post back your solution here, so it may help others in future. :) – Aravind Apr 05 '16 at 19:26
  • Yes, I meant multer. I wrote cloudinary by mistake. Will post here whatever they tell me. – codeinprogress Apr 06 '16 at 06:57
  • no luck with the multer team to get this sorted. I am using fs-extra module to delete all files from the upload folder as soon as the image is uploaded. It keeps the folder empty. – codeinprogress Jul 12 '16 at 16:04

5 Answers5

11

July 2018:

So if you want to store an image into some sort of database, you probably want to convert it into a buffer, and then insert buffer into the database. If you are using multer to process the multipart form (in this case the image file you want to upload), you can use multer's memoryStorage to get the buffer of the image directly. In other words:

var storage = multer.memoryStorage(); var upload = multer({ storage: storage });

to get the buffer:

When using memory storage, the file info will contain a field called buffer that contains the entire file.

that means you can get the buffer from command like req.files[0].buffer


I updated a simple repo demonstrating upload images to database without making a copy to local in this repo

skycocoo
  • 133
  • 1
  • 9
  • 1
    I took a look at the multer code (https://github.com/expressjs/multer) and I think that this is actually the default if you don't pass any options. So `var upload = multer();` should be equivalent to your suggestion. – jammartin Jan 13 '19 at 19:28
  • 1
    This is consistent with the documentation (https://www.npmjs.com/package/multer#multeropts): "Multer accepts an options object, the most basic of which is the dest property, which tells Multer where to upload the files. In case you omit the options object, the files will be kept in memory and never written to disk." – Paul Feb 20 '20 at 10:15
3

Prior to 14-07-2016 you could not configure multer to store files locally.

But multer is just a wrapper of busboy. So, we can try use busboy directly if we want to avoid hitting the disk.
Connect-bubsboy will help us:

app.use(busboy());

// request handler
req.busboy.on('file', function(fieldname, file, filename, encoding, mimetype) {
  file.on('end', function() {
    // do stuff with req.files[fieldname]
  });
});

Update 14-07-2016

Now multer has MemoryStorage to store files in memory as Buffer.

var storage = multer.memoryStorage()
var upload = multer({ storage: storage })
Tyler Slater
  • 127
  • 8
Ruslan Ismagilov
  • 1,360
  • 7
  • 14
3

From Multer info page at https://www.npmjs.com/package/multer

If the inMemory option is true - no data is written to disk but data is kept in a buffer accessible in the file object.

bFunc
  • 1,370
  • 1
  • 12
  • 22
  • 1
    When it says that one could 'run to app memory,' this is a limitation with server RAM maybe? Is it automatically 'cleared out,' or do we have to 'clean up; memory manually? – CodeFinity Jun 15 '20 at 18:36
0

You can also delete the file once it's uploaded to Cloudinary.

// used to delete images from local directory
const fs = require('fs'); // gain access to file system
const util = require('util');
const deleteFile = util.promisify(fs.unlink); // unlink will delete the file

// in your post request
app.post('/images', upload.single('myFile'), async function(req, res) {
  const file = req.file; // multer gives access to the file object in the request
  // code to upload the file to Cloudinary
  await deleteFile(file.path); // remove locally stored image by passing the file's path  
});
gig6
  • 307
  • 5
  • 16
-2

If you're storing images with Cloudinary then try using multer-storage-cloudinary

nax3t
  • 117
  • 4
  • 4
  • 50