【问题标题】:How do I transform an image using multer-s3?如何使用 multer-s3 转换图像?
【发布时间】:2021-02-07 22:41:35
【问题描述】:

我正在尝试在将图像上传到 Amazon S3 之前调整其大小。我找到了有关如何实现这一点的文档,但我怀疑我的实现不正确。我包括下面的代码。我做错了什么?

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: 'BUCKETNAME',
    contentType: multerS3.AUTO_CONTENT_TYPE,
  }),
  fileFilter: multerFilter,
  shouldTransform: function (req, file, cb) {
    cb(null, /^image/i.test(file.mimetype));
  },
  transforms: [
    {
      id: 'original',
      key: function (req, file, cb) {
        cb(null, 'image-original.jpg');
      },
      transform: function (req, file, cb) {
        cb(null, sharp().resize(100, 100).jpg());
      },
    },
  ],
});

【问题讨论】:

    标签: node.js amazon-s3 multer multer-s3


    【解决方案1】:

    我能够弄清楚这一点。下面的代码使用 npm 中的 multer-s3-image-transform 工作。

    const upload = multer({
      storage: multerS3({
        s3: s3,
        acl: 'public-read',
        bucket: 'BUCKETNAME',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        transforms: () =>
          sharp().resize(250, 250).jpeg({
            progressive: true,
            quality: 80,
          }),
      }),
      fileFilter: multerFilter,
    });
    

    【讨论】:

      猜你喜欢
      • 2017-05-22
      • 1970-01-01
      • 2021-02-04
      • 2017-03-22
      • 2020-08-10
      • 2021-04-04
      • 2019-10-10
      • 2021-06-10
      • 2017-04-22
      相关资源
      最近更新 更多