anacronw / multer-s3

multer storage engine for amazon s3
MIT License
660 stars 190 forks source link

Ability to integrate image processing - Sharp #81

Closed morettisf closed 3 years ago

morettisf commented 7 years ago

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?

ruandre commented 7 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?

morettisf commented 7 years ago

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))
      }
    }]
  })
})
Scobee commented 6 years ago

@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 ?

morettisf commented 6 years ago

@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) => {
})
huhuhong commented 6 years ago

I got error on transform function will sharp where 'file' is not define.

huhuhong commented 6 years ago

Problem solved.ignore my previous comment.thanks

Sulaiman90 commented 5 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?

Hi, Did you able to process images using jimp? If so, pls help.

LinusU commented 3 years ago

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...