1

I am using a third party service which pushes data to my expressjs endpoint to with a file. I have used multer and multer S3 to upload the file on S3 bucket and it works well when I try using postman.

However the when the third party makes a push to expressjs endpoint we get an error because express app expects req.file, and the third party request does holds it.

When I raised this concern with the third party they debugged the issue found:

Request sent by third party - Content-Disposition: form-data; name="csv_file"

But the express js app expects this - Content-Disposition: form-data; name="csv_file"; filename="some_file_name".

My middleware looks like this:

const AWS = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');
const logger = require('../../configs/winston-config');
const s3Config = require('../../configs/s3-config');

AWS.config.update({
  region: s3Config.region
});
const s3 = new AWS.S3();

const uploadCSVToS3Bucet = multer({
  storage: multerS3({
    s3: s3,
    bucket: s3Config.bucketName,
    fileFilter: function (req, file, cb) {
      file.mimetype === 'text/csv' ? cb(null, true) : cb(null, false);
    },
    metadata: function (req, file, cb) {
      cb(null, {fieldName: file.fieldname});
    },
    key: function (req, file, cb) {
      logger.info(file);
      cb(null, '<folder_name>/'+Date.now().toString());
    }
  })
});

module.exports = {
  uploadCSVToS3Bucet
};

Route:

router.post('/', use(intercept),use(uploadCSVToS3Bucet.single('csv_file')));

In the request object from the third party i get the following information in the body:

"body": {
            "batchId": "123",
            "fileName": "sample.csv",
            "reason": "REPORT"
        },

The third party is not going to make change by adding the filename in content-disposition as it might have regression, as mentioned here the filename is an optional parameter.

Now I am stuck on how to make this work in my expressjs app. I am not sure if absence of filename in content disposition is the reason that my file is not being uploaded to s3. If yes, how should i resolve it.

Apurva Mayank
  • 721
  • 2
  • 12
  • 32

0 Answers0