4

I am trying to upload file into AWS S3 using node js.

My requirement is to use dynamic value for bucket in multerS3 storage object

storage: multerS3({

        s3: s3,

        bucket:  function (req, file, cb) {
            console.log(" bucketName is >> "+JSON.stringify(req.bucketName));
            cb(null, req.bucketName)
        },

        limits : {
            fileSize : Number(Constants.UPLOADED_IMAGE_SIZE)
        },
        metadata: function (req, file, cb) {
            cb(null, { fieldName: file.fieldname });
        },

        key: function (req, file, cb) {
            cb(null, file.originalname)
        }
    })

I am trying to pass value through request object and use it in bucket paramater. but it is coming as undefined.

in both cases req.bucketName and req.body.bucketName is coming as undefined.

Need help to resolve this problem.

My postman request is as below

enter image description here

Thanks for any help.

ajoy sinha
  • 714
  • 1
  • 8
  • 19

1 Answers1

4

If you use Postman and have a file as a parameter, nothing else after it gets included.

Swap the order of your parameters, and have bucketName in front of the file and you will see it in the body.

The first picture shows an incorrect order, and body will not have a bucketName element. The second picture will pass through Busboy properly and the body will have a bucketName element of 'testName'.

This will have a blank body using Busboy and Node

body.bucketName will return 'testName'

Daniel Brown
  • 121
  • 7