Closed morettisf closed 3 years ago
I also want to modify the image (using jimp in my case) before saving it to S3. Is there any way to get the buffer and do something with it before passing it on to the storage engine?
Hey @ruandre I figured out a solution. Use this package instead of multer-s3: https://www.npmjs.com/package/multer-s3-transform/
Here's my setup:
const multer = require('multer');
const multerS3 = require('multer-s3-transform');
const aws = require('aws-sdk');
const sharp = require('sharp');
const awsAccessKey = process.env.AWS_KEY
const awsSecretKey = process.env.AWS_SECRET
aws.config.update({
secretAccessKey: awsSecretKey,
accessKeyId: awsAccessKey
})
var s3 = new aws.S3()
var upload = multer({
storage: multerS3({
s3: s3,
bucket: 'BUCKET_NAME_HERE',
contentType: multerS3.AUTO_CONTENT_TYPE,
shouldTransform: function (req, file, cb) {
cb(null, /^image/i.test(file.mimetype))
},
transforms: [{
id: 'original',
key: function (req, file, cb) {
let fileSplit = file.originalname.split('.')
let filename = fileSplit.slice(0, fileSplit.length - 1)
filename.push(Date.now())
filename = filename.join('_') + '.' + fileSplit[fileSplit.length - 1]
cb(null, filename)
},
transform: function (req, file, cb) {
cb(null, sharp().resize(1000))
}
}]
})
})
@morettisf
How do I set shouldTransform to true ?
I have this implementation, and use chRouter.post('/save', upload.single('logo'), (request: RequestCustom, response: Response, next: NextFunction) => {
but the response is:
{ fieldname: 'logo',
originalname: '1E9498B1-6126-4237-9029-0ABFBF44C6BD.jpg',
encoding: '7bit',
mimetype: 'image/jpeg',
size: 413277,
bucket: 'wlf',
key: '0756d64244bfbb76a6267c77981107c2',
acl: 'private',
contentType: 'application/octet-stream',
contentDisposition: null,
storageClass: 'STANDARD',
serverSideEncryption: null,
metadata: null,
location: 'https://.../0756d64244bfbb76a6267c77981107c2',
etag: '"99053578cd28a56e67fd5339ae40a912"' }
As you can see it wasn't transformed .. it just returns the normal image uploaded instead of the transformed array. What am I doing wrong here ?
@Scobee The way I checked it was looking in S3 and finding the transformed image in the bucket. I'm not quite sure how to advise in your situation but here is how I define my route in Express:
app.post('/image-submit', upload.single('file'), function (req, res, next) => {
})
I got error on transform function will sharp where 'file' is not define.
Problem solved.ignore my previous comment.thanks
I also want to modify the image (using jimp in my case) before saving it to S3. Is there any way to get the buffer and do something with it before passing it on to the storage engine?
Hi, Did you able to process images using jimp? If so, pls help.
Since this library streams the file directly to S3 it isn't easy to add this here...
I would recommend using Multer 3.x instead which should make this easy...
Hey, I wanted to see if there is currently a way to specify in the options to process the image(s) through an image processor like Sharp, to reduce the file size or dimensions?