Use multer-s3-transform middleware to handle S3 upload with file transformation like image resizing using sharp
const multer= require('multer')
const multerS3= require('multer-s3-transform')
const sharp= require('sharp')
const AWS= require('aws-sdk')
const S3= new AWS.S3({
accessKeyId= ...
secretAccessKey: ...
})
const upload= multer ({
storage multerS3{{
s3:S3;
bucket:....,
shouldTransform: true ,
transforms: [
{
id: 'original',
key=(req,file,cb)=> cb( null new Date().getTime() + '_' + req.file.originalname),
transform: (req, file, cb) => cb(null, sharp().jpg())
},
{
id: 'large',
key: (req, file, cb) => cb(null, new Date().getTime() + '_large_' + req.file.originalname),
transform: (req, file, cb) => cb(null, sharp().resize(1200, 900).jpg())
},
{
id: 'small',
key: (req, file, cb) => cb(null, new Date().getTime() + '_small_' + req.file.originalname),
transform: (req, file, cb) => cb(null, sharp().resize(400, 300).jpg())
}
]
})
})
router.post('/media', upload.single('media'))