Skip to content
  1. LuzineDebug
  2. DevOps, servers & cloud
  3. An error occurred (AccessDenied) when calling th…
DevOps, servers & cloud Intermediate ≈ 20 min to fix

Fix "An error occurred (AccessDenied) when calling the PutObject operation" on AWS S3

Error message
An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
  • upload failed: ./build/index.html to s3://my-bucket/index.html An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
  • An error occurred (AccessDenied) when calling the PutObject operation: User: arn:aws:iam::123456789012:user/deploy is not authorized to perform: s3:PutObject on resource: "arn:aws:s3:::my-bucket/index.html" because no identity-based policy allows the s3:PutObject action
  • botocore.exceptions.ClientError: An error occurred (AccessDenied) when calling the PutObject operation: Access Denied
  • AccessDenied: User: arn:aws:sts::123456789012:assumed-role/lambda-role/fn is not authorized to perform: kms:GenerateDataKey on resource: arn:aws:kms:eu-west-3:123456789012:key/1a2b3c4d
  • An error occurred (AccessDenied) when calling the ListObjectsV2 operation: Access Denied
In short

The AWS identity in use is not allowed to write to this bucket, or a rule explicitly prevents it. First check who you are with aws sts get-caller-identity, then that the IAM policy allows s3:PutObject on arn:aws:s3:::my-bucket/* (with /*), and that no bucket policy, Block Public Access or KMS key blocks it.

Be the first to confirm this fix

What this error means

For each request, AWS evaluates every applicable policy: the user’s or role’s IAM policy, the bucket policy, organisation SCPs, the VPC endpoint policy, and for an encrypted object the KMS key policy. The request is only accepted if at least one policy allows it and none explicitly denies it. So AccessDenied can come from any of those layers. Recent messages are more precise: because no identity-based policy allows points to the IAM policy, with an explicit deny in a resource-based policy points to the bucket policy. A bare "Access Denied" means checking layer by layer.

Common causes

  1. Wrong identity: the CLI uses another profile (AWS_PROFILE), leftover AWS_ACCESS_KEY_ID variables, or the instance role instead of the expected one.
  2. Wrong resource in the IAM policy: arn:aws:s3:::my-bucket is the bucket, objects are arn:aws:s3:::my-bucket/*; PutObject needs the latter.
  3. Explicit deny in the bucket policy: required encryption, HTTPS (aws:SecureTransport), VPC or source IP.
  4. Public ACL blocked: upload with --acl public-read while Block Public Access is enabled (the default).
  5. KMS key: the bucket encrypts with a customer-managed key and the identity lacks kms:GenerateDataKey (and kms:Decrypt for multipart uploads).

Step-by-step fix

  1. Check which identity is used

    Compare the displayed ARN with the user or role you granted rights to. Environment variables take precedence over ~/.aws/credentials.

    bash
    aws sts get-caller-identity
    aws configure list
    env | grep -E "^AWS_"
    aws sts get-caller-identity --profile deploy
  2. Fix the IAM policy

    Grant s3:PutObject on the objects (/*) and s3:ListBucket on the bucket itself. For aws s3 sync, both are needed, plus s3:DeleteObject with --delete.

    json
    {
      "Version": "2012-10-17",
      "Statement": [
        {
          "Effect": "Allow",
          "Action": ["s3:PutObject", "s3:GetObject", "s3:DeleteObject"],
          "Resource": "arn:aws:s3:::my-bucket/*"
        },
        {
          "Effect": "Allow",
          "Action": "s3:ListBucket",
          "Resource": "arn:aws:s3:::my-bucket"
        }
      ]
    }
  3. Simulate the permission

    The IAM simulator tells you whether the action is allowed by the identity’s policies, and which one blocks it. Replace the ARN with the one shown in step 1.

    bash
    aws iam simulate-principal-policy \
      --policy-source-arn arn:aws:iam::123456789012:user/deploy \
      --action-names s3:PutObject \
      --resource-arns "arn:aws:s3:::my-bucket/index.html" \
      --query "EvaluationResults[].[EvalActionName,EvalDecision]"
  4. Read the bucket policy and Block Public Access

    Look for an "Effect": "Deny" and its conditions. If you upload with --acl public-read, drop the option: for a public site, prefer CloudFront with Origin Access Control over disabling Block Public Access.

    bash
    aws s3api get-bucket-policy --bucket my-bucket --query Policy --output text
    aws s3api get-public-access-block --bucket my-bucket
    aws s3api get-bucket-encryption --bucket my-bucket
  5. Allow the KMS key

    If the bucket uses SSE-KMS with your own key, add these actions to the IAM policy and check that the key policy also allows the account or role.

    json
    {
      "Effect": "Allow",
      "Action": ["kms:GenerateDataKey", "kms:Decrypt"],
      "Resource": "arn:aws:kms:eu-west-3:123456789012:key/1a2b3c4d-5e6f-7a8b-9c0d-1e2f3a4b5c6d"
    }
  6. Test the upload again

    A small file is enough. IAM changes usually take effect within seconds.

    bash
    echo test > /tmp/test.txt
    aws s3 cp /tmp/test.txt s3://my-bucket/test.txt

Did this fix work for you?

Confirm it in one click: it will rise for the next developers stuck on the same error.

Keep it from coming back

  • Use one dedicated role per application or pipeline (OIDC for GitHub Actions) rather than shared long-lived access keys.
  • Keep Block Public Access enabled and serve public files through CloudFront.
  • Add aws sts get-caller-identity at the start of your deployment scripts to see right away which identity is acting.

Frequently asked questions

I am an account administrator and still get AccessDenied, how?

An explicit Deny always wins, even over AdministratorAccess: it comes from the bucket policy, an organisation SCP, a VPC endpoint policy or the KMS key policy.

Why does PutObject work but GetObject fail on the same file?

Rights are per action: you also need s3:GetObject. With SSE-KMS, reading also requires kms:Decrypt.

What about "AccessControlListNotSupported: The bucket does not allow ACLs"?

The bucket uses the "Bucket owner enforced" object ownership (default since 2023), which disables ACLs. Remove --acl or ACL from your code and manage access with policies.

Useful tools for this error

More DevOps, servers & cloud errors

All DevOps, servers & cloud errors →