I have encountered with multer and uploading to S3: I am sending 5 images in the following way:
app.post(
'/upload',
upload.fields(
[
{ name: 'image', maxCount: 1 },
{ name: 'image2', maxCount: 1 },
{ name: 'image3', maxCount: 1 },
{ name: 'image4', maxCount: 1 },
{ name: 'image5', maxCount: 1 }
]),
(req, res, next) => {
res.status(200).send('Files uploaded successfully.')
});
Using the following multer imlementation:
var upload = multer({
storage: multerS3({
s3: s3,
bucket: myBucket,
fileFilter: (res, file, callback) => {
var ext = path.extname(file.originalname);
if(ext !== '.png' && ext !== '.jpg' && ext !== '.jpeg') {
res.status(400).send('Only images are allowed')
}
callback(null, true)
},
acl: 'public-read',
contentType: multerS3.AUTO_CONTENT_TYPE,
key: function (req, file, cb) {
uuidFileName = uuidv4();
fileNamesArray.push(uuidFileName);
if (!file.originalname.match(/\.(jpg|jpeg|png|gif)$/)) {
return cb(new Error('Only image files are allowed!'));
}
cb(null, uuidFolderName + '/' + uuidFileName + '.' + mime.getExtension(file.mimetype))
}
})
})
My problem is that when a client send the fifth image as PDF for example, 4 images has already been uploaded to S3. While deleting them and cancelling the whole process is un-efficient, Iv'e been looking into multer's multiple upload option but without success, and no promise it will actually prevent before uploading the first one.
Any ideas?