Skip to content

Instantly share code, notes, and snippets.

@MorenoMdz
Created September 6, 2018 02:41
Show Gist options
  • Select an option

  • Save MorenoMdz/ccb169aaf7d39ad9b12cd5d709d83330 to your computer and use it in GitHub Desktop.

Select an option

Save MorenoMdz/ccb169aaf7d39ad9b12cd5d709d83330 to your computer and use it in GitHub Desktop.
Image upload, resize and rename with AWS S3 with node and Multer S3
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);
@mahmoudafer
Copy link
Copy Markdown

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!

@MorenoMdz
Copy link
Copy Markdown
Author

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!

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!

@ossycodes
Copy link
Copy Markdown

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 ?

@ossycodes
Copy link
Copy Markdown

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

@mahmoudafer
Copy link
Copy Markdown

mahmoudafer commented May 18, 2020

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

@detoner777
Copy link
Copy Markdown

Is there any option to use metadata(), with and height here?

@MorenoMdz
Copy link
Copy Markdown
Author

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.

@somadina94
Copy link
Copy Markdown

Shouldtransform still not working for me

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment