0

I am trying to crop images before it get send to s3 bucket. My issue is in my multerS3 options shouldTransform is being skipped hence not taking the transforms.

Here is my entire file of upload

require('multer-s3-transform');
const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const sharp = require('sharp');

aws.config.update({
    secretAccessKey: secretAccessKey,
    accessKeyId: accessKeyId,
    region: region,
});

const s3 = new aws.S3();

const fileFilter = (req, file, cb) => {
    if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png' || file.mimetype === 'image/jpg') {
        cb(null, true);
    } else {
        cb(new Error(message.FAIL.invalidImage), false);
    }
};

const upload = multer({
    fileFilter,
    storage: multerS3({
        s3,
        bucket: bucket,
        acl: 'public-read',
        shouldTransform: function (req, file, cb) {
            console.log('in should transform ');
            cb(null, true);
        },
        transforms: [
            {
                id: 'original',
                key: function (req, file, cb) {
                    cb(null, Date.now().toString());
                },
                transform: function (req, file, cb) {
                    console.log('og');
                    cb(null, sharp().jpg())
                },
            },
            {
                id: 'resized',
                key: function (req, file, cb) {
                    cb(null, Date.now().toString());
                },
                transform: function (req, file, cb) {
                    console.log('thumbnail');
                    cb(null, sharp().resize(300, 300).jpg())
                },
            }
        ],
        metadata: function (req, file, cb) {
            cb(null, {fieldName: 'some meta'});
        },
        key: function (req, file, cb) {
            cb(null, Date.now().toString());
        },
    })
});

module.exports = upload;

Here is my route

const photoUpload = upload.fields([{name: 'photo', maxCount: 1}]);
    // Route for uploading photo image
        app.post(routeRoot + '/upload/photo', function (req, res) {
            console.log('in route ');
            photoUpload(req, res, function (err) {
                if (err) {
                    return res.status(200).send({error: {message: err.message}});
                } else {
                    account.uploadPhoto(req, res);
                }
            })
        });

The result I get

files [Object: null prototype] {
  photo: [
    {
      fieldname: 'photo',
      originalname: '4.jpg',
      encoding: '7bit',
      mimetype: 'image/jpeg',
      size: 84154,
      bucket: '...',
      key: '...',
      acl: 'public-read',
      contentType: 'image/jpeg',
      contentDisposition: null,
      storageClass: 'STANDARD',
      serverSideEncryption: null,
      metadata: [Object],
      location: '...',
      etag: '...',
      versionId: undefined
    }
  ]
}

File upload to S3 works but it is not being transformed. I have been trying to figure this one out. Thank you!

kerubim
  • 137
  • 1
  • 8

1 Answers1

2

I've figured it out.

const multerS3 = require('multer-s3-transform');

instead of

const multerS3 = require('multer-s3');

for some reason the docs has 'multer-s3' require but not 'multer-s3-transform' which is what is needed for shouldTransform to work

kerubim
  • 137
  • 1
  • 8