anacronw/multer-s3

'Key' parameter not setting the key in s3

nomii15 opened this issue · 1 comments

The following is my code to upload a single file to aws s3. The param 'key' is not capitalized.

return multer({
    storage: multerS3({
      s3: s3,
      bucket: keys.S3_BUCKET_NAME,
      acl: aclType,
      key: function (req, file, cb) {
          cb(
            null,
            req.params.model +
              "/" +
              req.params.id +
              "/" +
              req.body.fileName +
              "/" +
              Date.now() +
              "-" +
              file.originalname
          );
      },
      cacheControl: "max-age=31536000",
      contentType: multerS3.AUTO_CONTENT_TYPE,
      metadata: function (req, file, cb) {
        cb(null, { fieldName: file.fieldname });
      },
      limits: { fileSize: 2000000 }, // In bytes: 2000000 bytes = 2 MB
      fileFilter: function (req, file, cb) {
        checkFileType(file, cb);
      },
    }),
  }).single("file");

When i try and upload a file through Postman it works as expected and the key that is set for the file follows the logic in the upload function above. However, when I try and upload an image with my React app, I get the following error...

MissingRequiredParameter: Missing required key 'Key' in params
    at ParamValidator.fail (C:\Users\\node_modules\aws-sdk\lib\param_validator.js:50:37)
    at ParamValidator.validateStructure (C:\Users\\node_modules\aws-sdk\lib\param_validator.js:61:14)
    at ParamValidator.validateMember (C:\Users\\node_modules\aws-sdk\lib\param_validator.js:88:21)
    at ParamValidator.validate (C:\Users\\node_modules\aws-sdk\lib\param_validator.js:34:10)
    at Request.VALIDATE_PARAMETERS (C:\Users\\node_modules\aws-sdk\lib\event_listeners.js:132:42)
    at Request.callListeners (C:\Users\\node_modules\aws-sdk\lib\sequential_executor.js:106:20)
    at callNextListener (C:\Users\\node_modules\aws-sdk\lib\sequential_executor.js:96:12)
    at C:\Users\\node_modules\aws-sdk\lib\event_listeners.js:86:9
    at finish (C:\Users\\node_modules\aws-sdk\lib\config.js:386:7)
    at C:\Users\Owner\\node_modules\aws-sdk\lib\config.js:404:9
    at Credentials.get (C:\Users\\node_modules\aws-sdk\lib\credentials.js:127:7)
    at getAsyncCredentials (C:\Users\\node_modules\aws-sdk\lib\config.js:398:24)
    at Config.getCredentials (C:\Users\\node_modules\aws-sdk\lib\config.js:418:9)
    at Request.VALIDATE_CREDENTIALS (C:\Users\\node_modules\aws-sdk\lib\event_listeners.js:81:26)
    at Request.callListeners (C:\Users\\node_modules\aws-sdk\lib\sequential_executor.js:102:18)
    at Request.emit (C:\Users\\node_modules\aws-sdk\lib\sequential_executor.js:78:10) {
  code: 'MissingRequiredParameter',
  time: 2021-10-01T08:51:54.311Z,
  storageErrors: []
}

I then changed the 'key' param to 'Key'. This silenced the error above and the image was uploaded successfully through Postman and my react app, however this time the key that was assigned was not matching my logic and seemed to just be a random string generated by aws. It seems that it has not recognized the 'Key' param.

Could you please submit a Minimal, Reproducible Example?