【问题标题】:Multer-s3-transform with Sharp middleware in Express JS route, uploads correctly, but never calls next在 Express JS 路由中使用 Sharp 中间件的 Multer-s3-transform 正确上传,但从不调用 next
【发布时间】:2020-09-03 09:00:43
【问题描述】:

我正在使用 multer-s3-transform 在上传到 S3 之前调整我的图片上传大小。使用 Sharp 调整大小。 转换工作正常,图像被调整大小并上传到 S3 存储桶,我可以看到存储桶中的文件。但是,uploadToS3.single('profilePic') 中间件永远不会继续到下一个路由,即结束路由。

我做错了什么?

exports.uploadToS3 = multer({
    storage: multerS3({
        s3: s3,
        bucket: S3_BUCKET,
        acl: 'public-read',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        fileFilter: allowOnlyImages,
        limits: {
            fileSize: 1024 * 250 // We are allowing only 250K
        },
        shouldTransform: (req, file, cb) => {
            // Is this an image file type?
            cb(null, /^image/i.test(file.mimetype));
        },
        transforms: [{
            id: 'thumbnail',
            key: (req, file, cb) => {
                const fn = `${S3_PROFILE_PICS}/${req.body.handle}/${createFilename(req, file)}`;
                //console.log('transforms key fn: ', fn);
                cb(null, fn);
            },
            transform: (req, file, cb) => {
                //console.log('transform: ', file.originalname);
                // Perform desired transformations
                const width = parseInt(PROFILE_PIC_W);
                const height = parseInt(PROFILE_PIC_H);
                cb(null, sharp()
                    .resize(width, height)
                    .toFormat('jpeg')
                );
            }
        }]
    })
});

路线...

router.post('/register', uploadToS3.single('profilePic'), async (req, res) => {
    ...
    ...
});

这与变换和/或夏普有关。如果我删除 Sharp 变换并将传入的图像上传到 S3,一切正常。

【问题讨论】:

    标签: express amazon-s3 multer-s3


    【解决方案1】:

    试试这个看看效果:

    router.post('/register', uploadToS3.single('profilePic'), async (req, res, next) => {
        ...
        ...
    });
    

    【讨论】:

      猜你喜欢
      • 2019-09-30
      • 2016-03-19
      • 2020-05-21
      • 2021-01-28
      • 2021-01-24
      • 2019-10-10
      • 2017-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多