Chr*_*ark 2 amazon-s3 node.js multer multer-s3
我已经能够使用 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,
}),
});
Run Code Online (Sandbox Code Playgroud)
我是个白痴。我在我的代码中看到了错误并已修复它。我不应该把它放在设置fileFilter里面storage。这是固定代码:
const upload = multer({
storage: multerS3({
s3: s3,
acl: 'public-read',
bucket: 'BUCKET NAME',
contentType: multerS3.AUTO_CONTENT_TYPE,
}),
fileFilter: multerFilter,
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2636 次 |
| 最近记录: |