Upload with dynamic Filename - multer/s3

1.6k Views Asked by At

I use multer/multer-s3 to upload a Profilepicture to Amazon S3. The Upload works well when i use a static filename like 'userPhoto_' which matches the the fieldname in the form.

var upload = multer({
  storage: multerS3({
      s3: s3,
      bucket: '<bucketname>',
      key: function (req, file, cb) {
          cb(null, file.fieldname);
      }
  })
});

router.post('/api/photo', upload.single('userPhoto_123'), function (req,res) {
  res.end("Uploaded!");
});

In my .ejs File i have the Input Field like

<input class="ui input" type="file" name="userPhoto_<%=person.id%>" />

So the question is how to pass the FieldName including the person.id to the upload.single - Middleware.

Thanks!

1

There are 1 best solutions below

0
On

modify bucket value as -

req: is normal express request

bucket: (req, file, cb) => { cb(null, ${req.dynamic_name}); };