【问题标题】:How do I restrict file types to images only using multerS3?如何仅使用 multerS3 将文件类型限制为图像?
【发布时间】: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


    【解决方案1】:

    我是个白痴。我在我的代码中看到了错误并已修复它。我不应该将fileFilter 放在storage 设置中。这是固定代码:

    const upload = multer({
      storage: multerS3({
        s3: s3,
        acl: 'public-read',
        bucket: 'BUCKET NAME',
        contentType: multerS3.AUTO_CONTENT_TYPE,
      }),
      fileFilter: multerFilter,
    });
    

    【讨论】:

      猜你喜欢
      • 2011-03-12
      • 2010-12-06
      • 1970-01-01
      • 2021-10-08
      • 1970-01-01
      • 1970-01-01
      • 2019-12-21
      • 2010-12-30
      • 2020-12-02
      相关资源
      最近更新 更多