anacronw / multer-s3

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

No Way to dynamic the bucket name, it's fixed here. #67

Closed shadikkhan closed 7 years ago

shadikkhan commented 7 years ago

As per the given example there is no way to provide bucket name dynamically. Scenario is something like user would send data from request with bucket name and that name can be passed in bucket name.

anacronw commented 7 years ago

Look at the documented key parameter that can be passed a function to generate the s3 key

shadikkhan commented 7 years ago

@badunk I am soryy, but did not get it as per my situation,

var upload = multer({ storage: multerS3({ s3: s3, bucket: 'some-bucket', metadata: function (req, file, cb) { cb(null, {fieldName: file.fieldname}); }, key: function (req, file, cb) { cb(null, Date.now().toString()) } }) }) in above code I have to set bucket : dynamicname

How can I do it? can you please help me little bit on this, i am new to this

anacronw commented 7 years ago

Oops sorry misread your question

bucket also accepts a function: https://github.com/badunk/multer-s3/blob/master/index.js#L81

shadikkhan commented 7 years ago

@badunk Thanks a ton, Resolved

javed24 commented 5 years ago

@AndroidHitchhiking I know it's late, but do you have an example of how you made the bucket accept a function? I've done the following but the upload doesn't work if I define bucket in this way:

bucket: (req, file, cb) => {
   let bucketName = null
   if (req.files[0].fieldname === 'type1') {
     bucketName = 'some-bucket'
    } else if (req.files[0].fieldname === 'type2') {
       bucketName = 'some-other-bucket'
    }
    return bucketName
},
LinusU commented 5 years ago

@javed24 instead of return bucketName you'll want to do cb(null, bucketName)

This is done in order to support asynchronously specifying a bucket name...

javed24 commented 5 years ago

@LinusU that works like a charm, awesome!