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

7
  • Troubleshoot -- Are your Config for S3 correct ? specifically the region ? Commented May 18, 2019 at 7: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 Commented May 18, 2019 at 7:57
  • Be sure about that. you can find your region through the amazon s3 url. Commented May 18, 2019 at 7:58
  • whatever key I have , I directly configure in aws.config.update() please check my question Commented May 18, 2019 at 7:59
  • @Subhajit even I checked from official documentaion docs.aws.amazon.com/general/latest/gr/rande.html Commented May 18, 2019 at 8:00

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

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'
});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.