2

I have been trying multer-s3 for many hours, But i'm failing in creating thumbnails. Can anyone please tell me how to run it?

Following is my code:

var upload = multer({
    storage: multerS3({
        s3: s3,
        bucket: 'test',
        // shouldTransform: function (req, file, cb) {
        //     cb(null, /^image/i.test(file.mimetype))
        // },
        acl: 'public-read',
        contentType: multerS3.AUTO_CONTENT_TYPE,
        shouldTransform : function (req, file, cb) {
            console.log('in should transform ', file)
            cb(null, /^image/i.test(file.mimetype))
        },
        transforms: [{
            id: 'original',
            key: function (req, file, cb) {
                console.log('original')
                cb(null, "original")
            },
            transform: function (req, file, cb) {
                console.log('original1')

                cb(null, sharp().jpg())
            }
        }, {
            id: 'thumbnail',
            key: function (req, file, cb) {
                console.log('thumbnail')

                cb(null, "thumbnail")
            },
            transform: function (req, file, cb) {
                console.log('thumbnail1')

                cb(null, file.resize(100, 100).jpg())
            }
        }]
    })
})


 app.post('/upload', upload.single('image'), extendTimeout, function(req, res, next) {
        console.log('filessss ', req.file )
        res.send('Successfully uploaded ' + req.file + ' files!')
    })

Original image is uploaded successfully but resized image is not. Can anyone please guide me?

Thanks

  • Hello, I am also working on the same. But I am getting this sharp(...).jpg is not a function. I already install a sharp pm. Can you please help me? – Vishnu Chauhan Apr 17 '20 at 12:54

1 Answers1

1

There is a typo in thumbnail's transform method. Instead of

cb(null, file.resize(100, 100).jpg())

should be

cb(null, sharp().resize(100, 100).jpg())
Alexander Schwarzman
  • 1,639
  • 1
  • 13
  • 17