I have nodejs app which uploads files to amazon s3, I am sending 2 separate type file values from frontend, one is single file and other is array of files so to handle this case i have this route setup
app.post('/api/class/add',
uploaders.multipleImageUpload.fields(
[
{
name: 'icon', maxCount: 1
},
{
name: 'galleryImages', maxCount: 15
}
]
),
ClassController.addClass);
my middleware function is this
const multipleImageUpload = multer({
storage: multerS3({
s3: s3,
bucket: awsConfig.s3.bucketName,
acl: 'public-read',
key: function(req, file, cb) {
cb(null,
path.basename(
file.originalname,
path.extname(file.originalname)
) + '-' + Date.now() + path.extname(file.originalname));
}
}),
limits: { fileSize: 10000000 }, // In bytes: 10000000 bytes = 10 MB
fileFilter: function(req, file, cb){
checkFileType(file, cb);
}
});
function checkFileType(file, cb){
// Allowed ext
const filetypes = /jpeg|jpg|png|gif|bmp/;
// Check ext
const extname = filetypes.test(path.extname(file.originalname).toLowerCase());
// Check mime
const mimetype = filetypes.test(file.mimetype);
if (mimetype && extname){
return cb(null, true);
} else {
cb('Error: Images Only!');
}
}
Now whenever i send request on above route only single file icon is uploaded to s3 bucket and then process moves to next function and there as well i see req.files array which only have one entry and that is icon file as well.
request data
name: abc
type: abc
description: abc
tags: abc,abc 2
classes: [object Object]
icon: (binary)
galleryImages: [object File],[object File],[object File],[object File],[object File]
and here is console.log(req.files)
[Object: null prototype] {
icon: [
{
fieldname: 'icon',
originalname: '1pcxst.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
size: 83109,
bucket: 'BUCKETNAME',
key: '1pcxst-1592071524750.jpg',
acl: 'public-read',
contentType: 'application/octet-stream',
contentDisposition: null,
storageClass: 'STANDARD',
serverSideEncryption: null,
metadata: null,
location: 'https://BUCKETNAME.s3.amazonaws.com/1pcxst-1592071524750.jpg',
etag: '"f3ce110e00f7ebfe2f18c77f323fdd0b"',
versionId: undefined
}
]
}
I have checked checkFileType is called only once.