Multer: shouldTransform being skipped

Created on 30 Aug 2018  路  7Comments  路  Source: expressjs/multer

const s3 = new AWS.S3();
const maxSize = 5 * 1000 * 1000;
/* TODO Resize */
const multerOptions = {
  storage: multerS3({
    s3: s3,
    bucket: process.env.AWS_BUCKET,
    bucketPath: 'images',
    acl: 'public-read',
    limits: {
      fileSize: maxSize,
      files: 5,
      fields: 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;
      fileName = `${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);

This is my temp code, trying to have the resize to work. I tried many different codes but it doesn't look like the transforms array is being loaded at all. Is there a way I can call sharp() earlier and have multer upload the resized images to s3 then?

Most helpful comment

o u gotta use this
npm install multer-s3-transform

All 7 comments

any luck? same thing is happening to me

o u gotta use this
npm install multer-s3-transform

shouldTransform is not part this library, you might have some luck with multer-s3-transform as previously mentioned 鈽猴笍

I have installed multer-s3-transform but shouldTransform is still being skipped? I have been trying to figure this out for a while now. Help?

+1

same issue :(

You need to install multer-s3-transform as mentioned above. make sure to use it instead of using your old multer-s3 package.
just do const multerS3 = require('multer-s3-transform') instead of const multerS3 = require('multer-s3') and you should be good to go :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

josephstgh picture josephstgh  路  3Comments

ChristianRich picture ChristianRich  路  4Comments

adrienbarreau picture adrienbarreau  路  3Comments

tonghae picture tonghae  路  4Comments

Gabxi picture Gabxi  路  3Comments