2

I'm using multer s3 to upload images from my node.js app to my amazon bucket. I'm using the date together with the original filename to create the new filename; however, when I try to display the new filename it just displays as undefined. This is my code:

    var upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'imagebucket',
    acl: 'public-read',
    metadata: function (req, file, cb) {
      cb(null, Object.assign({}, req.body));
    },
    key: function (req, file, cb) {
        console.log(file);
        cb(null, Date.now()+file.originalname);
    }
  })
})

app.post('/upload', upload.single('upload'), function(req, res, next) {
  res.send('Successfully uploaded ' + req.file.length + ' files!')
})

Any help would be welcomed.

Damian Buttle
  • 75
  • 3
  • 11
  • Exactly where do you try to display filename? In your code, it seems you only try to display file object that passed down to the multer – reyalsnogard Jun 03 '18 at 10:36

2 Answers2

1

It looks like you are not passing a string to the key function

Please try this:

key: function (req, file, cb) {
    console.log(file);
    cb(null, `${Date.now().toString()}${file.originalname}`);
}
AnC
  • 641
  • 4
  • 16
0

Try req.file.location in the last function middle ware

user6016061
  • 105
  • 4