2

I'm trying to upload files to my s3 bucket, using multer and multer-s3 for Nodejs. The problem that I have now is that I want to set up my s3 instance dynamically because the s3 account and the bucket depend on my user settings.

I have the following code:

My uploader

var uploader = multer({
storage: multerS3({
    s3: function(req, file, cb){
     cb(null, new AWS.S3({
      // my s3 settings from req.body or getting directly from my db
     }))
    },
    bucket: function (req, file, cb){
        cb(null, req.body.bucket)
    },
    metadata: function (req, file, cb) {
        cb(null, {
            fieldName: file.fieldname
        });
    },
    key: function (req, file, cb) {
        console.log(`Key is ${req.body.prefix + file.originalname}`);
        cb(null, req.body.prefix + file.originalname)
    }
  })
}).single('file');

api.post('/upload', uploader, function (req, res, next) {
  if (err)
   return res.status(500).json({ message: err });

  res.status(200).json({
    message: 'uploaded'
  });
});

The problem is that multer-s3 doesn't allow a function for s3 option as parameter but an s3 object instead.

How can I aproach this?

0 Answers0