【发布时间】:2021-02-07 21:01:02
【问题描述】:
我已经能够使用 multerS3 将我的 S3 存储桶连接到我的 Web 应用程序,但是,我不确定如何限制只能上传到图像的文件。在我使用 S3 之前,我使用 multerFilter 来检查 mime 类型,这很有效。但是,现在,使用相同的代码,似乎任何类型的文件都会上传到我的 S3 存储桶。使用 multerS3 将文件类型限制为图像的最佳方法是什么?谢谢。
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,
}),
});
【问题讨论】:
标签: node.js amazon-s3 multer multer-s3