Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

InvalidAccessKeyId while trying to upload to s3

I'm using the aws-sdk to upload files to s3. I'm configuring my credentials with

aws.config.update({
  accessKeyId: aws.config.credentials.accessKeyId,
  secretAccessKey: aws.config.credentials.secretAccessKey,
  region: 'us-east-1'
});

Then uploading with multer-s3:

const s3 = new aws.S3();

const upload = multer({
  storage: multerS3({
    s3: s3,
    bucket: 'my-bucket-v1',
    acl: 'public-read',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    key: function (req, file, cb) {
      const today = new Date();
      cb(null, file.originalname)
      console.log("file\n", file);
    }
  })
}).array('upl', 1);

router.post('/api/upload', (req, res, next) => {
  upload(req, res, err => {
    if (err) return console.log("err\n", err);
    res.status(201).send();
  })
});

The error I keep getting is with my Access Key Id: "The AWS Access Key Id you provided does not exist in our records."

I've created multiple new access keys in my aws account, but nothing works. I'm using root user access keys (I tried an IAM user, and it still didn't work).

I also logged my aws credentials in my node server (console.log(s3)), and it matches what's in my aws security credentials.

How do I properly configure my aws credentials to upload to s3?

like image 627
Farid Avatar asked Dec 01 '25 02:12

Farid


2 Answers

Finally figured it out. I had to set the access keys where I was initializing s3 before using it in the multer object.

const s3 = new aws.S3({
  accessKeyId: ACCESS_KEY_ID // Set access key here
  secretAccessKey: SECRET_ACCESS_KEY,
});

const upload = multer({
  storage: multerS3({
    s3: s3, // Use s3 instance here
    bucket: 'my-match',
    acl: 'public-read',
    contentType: multerS3.AUTO_CONTENT_TYPE,
    key: function (req, file, cb) {
      const today = new Date();
      cb(null, file.originalname)
      console.log("file\n", file);
    }
  })
}).array('upl', 1);

I still don't understand why the previous implementation doesn't work anymore, but at least this way works.

like image 149
Farid Avatar answered Dec 02 '25 21:12

Farid


Seems like AWS is not able to find the accessKey you are providing to the AWS config.

Verify your config.

aws.config.update({
  accessKeyId: aws.config.credentials.accessKeyId,
  secretAccessKey: aws.config.credentials.secretAccessKey,
  region: 'us-east-1'
});

Read more about accessKeyId and secretAccessKey here:

https://aws.amazon.com/blogs/security/wheres-my-secret-access-key/

like image 23
Harshal Y. Avatar answered Dec 02 '25 19:12

Harshal Y.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!