1

I'm trying to upload a file to s3 but it not happening what I expected. I create a file-helper.js in middleware, that is looks like below

const aws = require('aws-sdk');
const multer = require('multer');
const multerS3 = require('multer-s3');

aws.config.update({
        accessKeyID:'XXXXXXXXXXXXXX',    
        SecretAccessKey:'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
        region:'ap-south-1'
});

const s3 = new aws.S3();

const fileFilter = (req, file, cb) => {
    if (file.mimetype === 'image/jpeg' || file.mimetype === 'image/png') {
      cb(null, true);
    } else {
      cb(new Error('Invalid file type, only JPEG and PNG is allowed!'), false);
    }
  }

const upload = multer({
    fileFilter,
    storage: multerS3({
      s3,
      bucket: 'demo.moveies.com',
      acl: 'public-read',
      metadata: function (req, file, cb) {
        cb(null, {fieldName: file.fieldname});
      },
      key: function (req, file, cb) {
        cb(null, Date.now().toString())
      }
    })
  })

  module.exports = upload;

and my controller file like below

const upload = require('../middleware/file-helper');
const imageUpload = upload.single('image');
exports.fileUpload = async(req,res)=>{
    imageUpload(req, res, function(err, some) {
        if (err) {
          return res.status(422).send({errors: [{title: 'Image Upload Error', detail: err.message}] });
        }
        return res.json({'imageUrl': req.file.location});
      });
}

when hit the API end point it is giving error

{ "errors": [ { "title": "Image Upload Error", "detail": "Missing credentials in config" } ] }

I'm not able to figure out where I went in wrong in my code. can one help me in this situation

change need
  • 137
  • 2
  • 6
  • 23
  • Troubleshoot -- Are your Config for S3 correct ? specifically the region ? –  May 18 '19 at 07:52
  • my s3 bucket region is `Asia Pacific(mumbai)` so I mentioned as `ap-south-1` .. and remaining `accessKeyID` and `SecretAccessKey` same as s3 bucket – change need May 18 '19 at 07:57
  • Be sure about that. you can find your region through the amazon s3 url. –  May 18 '19 at 07:58
  • whatever key I have , I directly configure in `aws.config.update()` please check my question – change need May 18 '19 at 07:59
  • @Subhajit even I checked from official documentaion https://docs.aws.amazon.com/general/latest/gr/rande.html – change need May 18 '19 at 08:00
  • I believe then your code should run..try this link and see if it helps https://stackoverflow.com/questions/40494050/uploading-image-to-amazon-s3-using-multer-s3-nodejs –  May 18 '19 at 08:05
  • I tried still it is not working :( – change need May 18 '19 at 08:11

2 Answers2

1

There are typos in your config details. It should be accessKeyId not accessKeyID and secretAccessKey and not SecretAccessKey.

1

You have used the wrong key SecretAccessKey and accessKeyID, try changing it to secretAccessKey and accessKeyId.

aws.config.update({
    accessKeyId:'XXXXXXXXXXXXXX',    
    secretAccessKey:'XXXXXXXXXXXXXXXXXXXXXXXXXXX',
    region:'ap-south-1'
});
hashed_name
  • 553
  • 6
  • 21