Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use dynamic bucket name in Multer-s3 for file upload

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.

like image 933
ajoy sinha Avatar asked May 22 '18 12:05

ajoy sinha


People also ask

How do I import a CSV file into an S3 bucket?

Navigate to All Settings > Raw Data Export > CSV Upload. Toggle the switch to ON. Select Amazon S3 Bucket from the dropdown menu. Enter your Access Key ID, Secret Access Key, and bucket name.

Can we update file in S3 bucket?

It's not possible to append to an existing file on AWS S3. You can delete existing file and upload new file with same name.

How many ways you can upload data to S3?

There are three ways in which you can upload a file to amazon S3.


1 Answers

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'

like image 168
Daniel Brown Avatar answered Oct 04 '22 21:10

Daniel Brown