-
-
Save MorenoMdz/ccb169aaf7d39ad9b12cd5d709d83330 to your computer and use it in GitHub Desktop.
const multer = require('multer'); | |
const multerS3 = require('multer-s3'); | |
const uuid = require('uuid'); | |
const AWS = require('aws-sdk'); | |
const sharp = require('sharp'); | |
AWS.config.update({ | |
accessKeyId: process.env.AWS_ACCESS, | |
secretAccessKey: process.env.AWS_SECRET_KEY | |
}); | |
const s3 = new AWS.S3(); | |
const maxSize = 5 * 1000 * 1000; | |
/* TODO Resize */ | |
const multerOptions = { | |
storage: multerS3({ | |
s3: s3, | |
bucket: process.env.AWS_BUCKET, | |
acl: 'public-read', | |
limits: { | |
fileSize: maxSize, | |
files: 5 | |
}, | |
contentType: multerS3.AUTO_CONTENT_TYPE, | |
key: function(req, file, cb) { | |
const extension = file.mimetype.split('/')[1]; // gets the extension | |
const area = req.body.area; | |
const destinationFolder = req.body.destinationFolder; | |
fileName = `${destinationFolder}/${area}${uuid.v4()}.${extension}`; | |
cb(null, fileName); | |
}, | |
shouldTransform: function(req, file, cb) { | |
cb(null, /^image/i.test(file.mimetype)); | |
}, | |
transforms: [ | |
{ | |
id: 'original', | |
transform: function(req, file, cb) { | |
//Perform desired transformations | |
cb( | |
null, | |
sharp() | |
.resize(600, 600) | |
.max() | |
); | |
} | |
} | |
], | |
fileFilter(req, file, next) { | |
const isPhoto = file.mimetype.startsWith('image/'); | |
if (isPhoto) { | |
next(null, true); // null for error means it worked and it is fine to continue to next() | |
} else { | |
next({ message: 'Fotos: Tipo de arquivo não suportado!' }, false); // with error | |
} | |
} | |
}) | |
}; | |
exports.upload = multer(multerOptions).array('photos', 5); |
Hi, this was very helpful thank you, but i wanna ask, where did you find the properties "fileFilter", "transforms", "shouldTransform", and "limits"? I couldn't find them anywhere!
Hey man did this transformation work for you ?
Hi, this was very helpful thank you, but i wanna ask, where did you find the properties "fileFilter", "transforms", "shouldTransform", and "limits"? I couldn't find them anywhere!
Hey man did this transformation work for you ?
as regards the fileFilter and limits options they come from the multer package, you can find it explained here: https://www.npmjs.com/package/multer, the transforms and shouldTransform options come multer-s3-transform, you can read it up here https://www.npmjs.com/package/multer-s3-transform
Hi, this was very helpful thank you, but i wanna ask, where did you find the properties "fileFilter", "transforms", "shouldTransform", and "limits"? I couldn't find them anywhere!
Hey man did this transformation work for you ?
hi ossycodes! actually no it didin't, took a lot of time with no results so ended up using jimp with busboy instead of sharp & multer
Is there any option to use metadata(), with and height here?
Is there any option to use metadata(), with and height here?
Hey, not sure, this is quite old, to be honest, you might want to try a different approach as Mahmoudafer suggested.
Shouldtransform still not working for me
Hi there, as this is a bit old I am not sure where I got the info by the time, I think there is an updated version of multer, give it a check!