I have been able to connect my S3 bucket to my web application using multerS3, however, I'm not sure how to go about restricting the files that can be uploaded to images only. Before I was using S3, I used multerFilter to check the mime type, and that worked. However, now, using the same code, it seems like any type of file will be uploaded to my S3 bucket. What's the best way to restrict file types to images with multerS3? Thank you.
const multerFilter = (req, file, cb) => {
if (file.mimetype.startsWith('image')) {
cb(null, true);
} else {
cb(new AppError('Not an image! Please upload images only.', 400), false);
}
};
const upload = multer({
storage: multerS3({
s3: s3,
acl: 'public-read',
bucket: 'BUCKET NAME',
contentType: multerS3.AUTO_CONTENT_TYPE,
fileFilter: multerFilter,
}),
});
I'm an idiot. I see the error in my code and have fixed it. I should not have put
fileFilter
inside of thestorage
setting. This is the fixed code: