multer: Memory storage option is not working.
I am using multer to upload images to my cloudinary account. Since the images will be uploaded to cloud I do not need them to be stored locally in my app. I am using the MemoryStorage option as given in the documentation.
var cloudinary = require('cloudinary');
var multer = require('multer');
var storage = multer.memoryStorage();
var upload = multer({ storage: storage });
var cloudinaryConfig = require('../config/cloudinary');
router.post('/', upload.single('image'), function(req, res){
cloudinary.config(cloudinaryConfig.connection);
cloudinary.uploader.upload(req.file.path,{tags:'basic_sample'},function(err,image){
if(err){
console.warn('------------------- ERROR: ' + err);
}
console.log("* public_id for the uploaded image is generated by Cloudinary's service.");
console.log("* "+image.public_id);
console.log("* "+image.url);
});
});
However while uploading the image is also uploaded to an “uploads” folder. Any idea what the problem is? Thank you.
About this issue
- Original URL
- State: closed
- Created 8 years ago
- Comments: 17 (3 by maintainers)
i was able to upload it with memory storage. Cloudinary does not directly upload file buffer, i had to covert the buffer into a datauri.
Sorry for the delay on this, I think what’s happening is that you already have another multer middleware running before the
upload.single('image')
one. The memory storage doesn’t provide areq.file.path
property, but instead gives you a buffer atreq.file.buffer
.Can you show how the
router
object is being used?The following small example works for me:
@adeelibr There are some instances where you are unable to write to the server’s filesystem (ex. Heroku’s free tier) in that case memoryStorage is the way to go
You need to use the
datauri
package @adeelibrI was able to do it like this.