S3 says Access Denied when my Node.js code reads or writes an object
AccessDeniedShort 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.
Fix it fast
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.csv2Match what you see, then jump to its fix
- 1
because 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
AccessDenied 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
AccessDenied 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
with 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
The 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
- 1
What does the error look like?
AccessDeniedAccess DeniedUser: arn:aws:iam::123456789012:user/MaryMajor is not authorized to perform: s3:GetObject because no identity-based policy allows the s3:GetObject actionwith 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-bucket2Fix
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.csvConfirmed by docs.aws.amazon.com
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.csv2Fix
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.txtConfirmed by docs.aws.amazon.com
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.csv2Fix
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.csvConfirmed by docs.aws.amazon.com
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-bucket2Fix
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.json3Verify
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.csvConfirmed by docs.aws.amazon.com
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-identity2Fix
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_TOKEN3Verify
Restart the Node.js process and read the user ARN in the next error, or the successful response.
node app.jsConfirmed by docs.aws.amazon.com
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
- Amazon S3: troubleshoot access denied (403 Forbidden) errors
- Amazon S3: required permissions for S3 API operations
- Amazon S3 API reference: GetObject
- AWS SDK for JavaScript v3: set credentials in Node.js
- AWS SDKs and Tools: AWS access keys
- Service Authorization Reference: Amazon S3 resource types
- IAM: policy Version element
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:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.
Related fixes
Back end
S3 returns 403 when you upload with a presigned URL
AccessDenied
A 403 from a presigned URL means S3 refused the signature, the permissions behind it, or the credentials' lifetime.
3 causes, each with a fixBack end
S3 PutObject fails in PHP with AccessDenied or AccessControlListNotSupported
because no identity-based policy allows the s3:PutObject action
A failed putObject() carries an HTTP status and an S3 error code in its message.
4 causes, each with a fixBack 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 fixBack end
AWS SDK for JavaScript v3 cannot find credentials, even for DynamoDB Local
A DynamoDB client pointed at http://localhost:8000 fails with the same message
The SDK walked its default credential provider chain and found nothing.
3 causes, each with a fixBack end
AWS SDK says the security token included in the request is invalid
The security token included in the request is invalid
The call reached AWS but the credentials it carried were refused.
3 causes, each with a fixBack end
Amazon Cognito says the user pool client does not exist
ResourceNotFoundException (HTTP status code 400) returned by InitiateAuth
Cognito looks the app client ID up inside the Region you call.
3 causes, each with a fix