Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can we configure multer to not store files locally and only for using req.files.image.path?

Tags:

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?

like image 705
Aravind Avatar asked Aug 05 '14 16:08

Aravind


People also ask

What are the options that can be passed to multer?

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. By default, Multer will rename the files so as to avoid naming conflicts.

Where does multer save file?

Before using Multer to handle the upload action of files, we need to understand a few things. The actual files are never stored in the database. They are always stored someplace on the server. In our tutorial, we will store the uploaded files in the public folder.

Can I use multer without Express?

You cannot use Multer without because it's Express middleware.

How do I use multer to file?

The following code will go in the app.const multer = require('multer'); const upload = multer({dest:'uploads/'}). single("demo_image"); Here, we have called the multer() method. It accepts an options object, with dest property, which tells Multer where to upload the files.


2 Answers

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

like image 165
skycocoo Avatar answered Nov 02 '22 13:11

skycocoo


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.

like image 27
bFunc Avatar answered Nov 02 '22 12:11

bFunc