Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

S3 says Access Denied when my Node.js code reads or writes an object

ErrorAccessDenied

Short answer

S3 answers AccessDenied when no policy allows the exact action on the exact resource, when something denies it, or when the SDK signs with a different identity than you think. Read the full error message first: it names the user, the action and the policy type. Then check the policy resource ARN, s3:ListBucket, KMS permissions and which credentials the SDK resolved.

  • Applies to AWS SDK for JavaScript v3 (@aws-sdk/client-s3), Node.js 22, Amazon S3 general purpose buckets
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the whole error from the SDK call, then ask AWS which identity a set of credentials belongs to. For requests inside one account or organization, the message names the user, the action, the resource and the type of policy that said no. Cross-account requests only get a plain Access Denied.

    aws sts get-caller-identity
    aws s3api head-object --bucket my-bucket --key reports/a.csv
  2. 2Match what you see, then jump to its fix

    1. 1because no identity-based policy allows the s3:GetObject action, although the policy lists the bucketThe policy names the bucket, but the action works on objects (or the other way round)Fix
    2. 2AccessDenied for a key that you are not sure is there, while other keys workThe key does not exist and the identity has no s3:ListBucketFix
    3. 3AccessDenied only for objects in a bucket whose default encryption is SSE-KMS with a customer managed keyThe object is encrypted with a KMS key the identity may not useFix
    4. 4with an explicit deny in a resource-based policy (or a service control policy, permissions boundary, session policy or VPC endpoint policy)Another policy layer denies the requestFix
    5. 5The user ARN in the error is not the IAM user or role you gave permissions toThe SDK signed the request with other credentials than you expectedFix

What does the error look like?

  • AccessDenied
  • Access Denied
  • User: arn:aws:iam::123456789012:user/MaryMajor is not authorized to perform: s3:GetObject because no identity-based policy allows the s3:GetObject action
  • with an explicit deny in a resource-based policy

Cause 1 of 5

The policy names the bucket, but the action works on objects (or the other way round)

1Check

S3 splits its actions by resource type. Object actions such as GetObject and PutObject work on the object resource, whose ARN is the bucket ARN plus a slash and the key. Bucket actions such as ListBucket, which also backs ListObjectsV2 and HeadBucket, work on the bucket resource, whose ARN has no key part. A policy that gives an object action only the bucket ARN matches nothing.

aws sts get-caller-identity
aws s3api get-bucket-policy --bucket my-bucket

2Fix

Give each group of actions the ARN type it needs: the bucket ARN for ListBucket, and the bucket ARN with /* for the object actions.

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Effect": "Allow",
      "Action": ["s3:ListBucket"],
      "Resource": "arn:aws:s3:::my-bucket"
    },
    {
      "Effect": "Allow",
      "Action": ["s3:GetObject", "s3:PutObject"],
      "Resource": "arn:aws:s3:::my-bucket/*"
    }
  ]
}

3Verify

Run the same call again with the same credentials. It should return the object instead of AccessDenied.

aws s3api get-object --bucket my-bucket --key reports/a.csv /tmp/a.csv

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 5

The key does not exist and the identity has no s3:ListBucket

1Check

When an object is missing, S3 chooses the status by the caller's permissions. With s3:ListBucket on the bucket, the answer is 404 Not Found. Without it, the answer is 403 Access Denied, so a typo in the key or a wrong prefix looks like a permissions problem.

aws s3api head-object --bucket my-bucket --key reports/a.csv
aws s3api head-object --bucket my-bucket --key reports/not-there.csv

2Fix

Add s3:ListBucket on the bucket ARN so a missing key returns NoSuchKey, then correct the key or the prefix in your code.

{
  "Effect": "Allow",
  "Action": ["s3:ListBucket"],
  "Resource": "arn:aws:s3:::my-bucket"
}

3Verify

Request a key that does not exist. You should now see a 404 and not a 403.

aws s3api head-object --bucket my-bucket --key no-such-key.txt

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 5

The object is encrypted with a KMS key the identity may not use

1Check

Look at the bucket's encryption setting and at the object. With a customer managed key, S3 permissions alone are not enough: the identity must also be allowed to use the key.

aws s3api get-bucket-encryption --bucket my-bucket
aws s3api head-object --bucket my-bucket --key reports/a.csv

2Fix

Allow kms:GenerateDataKey for uploads and kms:Decrypt for downloads and multipart uploads, both in the identity's policy and in the KMS key policy. With the AWS managed key aws/s3, the caller must belong to the account that owns that key.

{
  "Effect": "Allow",
  "Action": ["kms:GenerateDataKey", "kms:Decrypt"],
  "Resource": "arn:aws:kms:eu-west-1:123456789012:key/KEY-ID"
}

3Verify

Download the object again with the same credentials.

aws s3api get-object --bucket my-bucket --key reports/a.csv /tmp/a.csv

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 5

Another policy layer denies the request

1Check

An explicit Deny statement in any of these layers blocks the request. The message tells you which kind of policy denied the call and, for most kinds, gives the policy ARN. Read the bucket policy for a Deny that matches your user or role, and ask whoever runs your AWS Organization about SCPs and resource control policies.

aws s3api get-bucket-policy --bucket my-bucket

2Fix

Change or narrow the Deny statement that matches (for example with a condition that excludes your role), or add the missing Allow to the layer named in the message. Update that layer, not the identity policy.

# for a bucket policy: edit policy.json, then apply it
aws s3api put-bucket-policy --bucket my-bucket --policy file://policy.json

3Verify

Repeat the failed call. A message that names a different layer means the first one is fixed and the next one denies.

aws s3api head-object --bucket my-bucket --key reports/a.csv

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 5 of 5

The SDK signed the request with other credentials than you expected

1Check

The SDK for JavaScript v3 uses a default credential provider chain and stops at the first source that returns credentials. Values set in code come first, then environment variables, then the shared config and credentials files. A stale AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY or AWS_SESSION_TOKEN in the shell or in the process manager therefore wins over the profile you configured.

printenv | grep -o '^AWS_[A-Z_]*'
aws sts get-caller-identity

2Fix

Remove the stale variables from the environment (shell profile, .env file loader, container or process manager settings), or give the identity you really use the permissions. Do not rely on more than one credential source at once.

unset AWS_ACCESS_KEY_ID AWS_SECRET_ACCESS_KEY AWS_SESSION_TOKEN

3Verify

Restart the Node.js process and read the user ARN in the next error, or the successful response.

node app.js

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Write bucket policies with two statements: bucket ARN for the bucket actions, bucket ARN plus /* for the object actions.
  • Give the runtime identity s3:ListBucket so a wrong key gives a clear 404, not a 403.
  • Run aws sts get-caller-identity from the same environment as the app when you debug, never from your laptop alone.
  • Log error.name and error.$metadata.httpStatusCode next to the key and bucket for every failed S3 call.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

The AWS SDK in Node.js cannot find any credentials

Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

The AWS SDK walks a fixed credential chain: code, environment variables, the shared files in ~/.aws, then role sources such as ECS or EC2.

3 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.