Maaz Zindani

Loading the index…

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

S3 PutObject fails in PHP with AccessDenied or AccessControlListNotSupported

ErrorAn error occurred (AccessDenied) when calling the PutObject operation

Short answer

A failed putObject() carries an HTTP status and an S3 error code in its message. Catch S3Exception and read it. A 403 AccessDenied points to IAM or bucket policy, or to Block Public Access when the upload sends a public ACL. A 400 AccessControlListNotSupported means the bucket has ACLs switched off, so drop the ACL parameter.

  • Applies to PHP 8.3, aws-sdk-php 3, Amazon S3
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Catch S3Exception around the call and print its message. The text names the missing permission, the ACL error or the Block Public Access setting, which tells the causes below apart.

    <?php
    use Aws\S3\Exception\S3Exception;
    try {
        $s3->putObject(['Bucket' => $bucket, 'Key' => $key, 'SourceFile' => $path]);
    } catch (S3Exception $e) {
        echo $e->getMessage();
    }
  2. 2Match what you see, then jump to its fix

    1. 1403 AccessDenied with the phrase because no identity-based policy allows the s3:PutObject actionThe IAM user or role has no s3:PutObject permissionFix
    2. 2400 with the error code AccessControlListNotSupportedThe upload sends an ACL, but the bucket has ACLs disabledFix
    3. 3AccessDenied with because public ACLs are prevented by the BlockPublicAcls setting in S3 Block Public AccessBlock Public Access rejects a public ACLFix
    4. 4A CredentialsException, or a generic credentials error from the last provider in the chainThe client has no region or no credentialsFix

What does the error look like?

  • An error occurred (AccessDenied) when calling the PutObject operation
  • because no identity-based policy allows the s3:PutObject action
  • AccessControlListNotSupported
  • because public ACLs are prevented by the BlockPublicAcls setting in S3 Block Public Access

Cause 1 of 4

The IAM user or role has no s3:PutObject permission

1Check

S3 treats a missing Allow as an implicit deny. When a Deny statement is involved, the message says explicit deny and names the policy. Confirm which identity the code runs as, then read that identity's policies and the bucket policy.

aws sts get-caller-identity

2Fix

Allow s3:PutObject on the bucket's objects for that identity, and remove or narrow any Deny statement the message points to.

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

3Verify

Repeat the upload from the CLI with the same identity.

aws s3api put-object --bucket amzn-s3-demo-bucket --key test.txt --body test.txt

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 2 of 4

The upload sends an ACL, but the bucket has ACLs disabled

1Check

New buckets use the Bucket owner enforced setting, which disables ACLs. Such a bucket accepts only uploads that send no ACL, or the bucket-owner-full-control ACL. Look for an ACL key in your call, for example public-read.

grep -rn "'ACL'" --include=*.php .
aws s3api get-bucket-ownership-controls --bucket amzn-s3-demo-bucket

2Fix

Remove the ACL parameter from putObject() and control access with a bucket policy or IAM policy. Only re-enable ACLs on the bucket if you truly need per-object ACLs.

<?php
$s3->putObject([
    'Bucket'     => $bucket,
    'Key'        => $key,
    'SourceFile' => $path,
]);

3Verify

The upload returns a result instead of a 400.

php upload.php

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 3 of 4

Block Public Access rejects a public ACL

1Check

BlockPublicAcls makes PutObject fail when the request includes a public ACL. All four Block Public Access settings are on by default for new buckets.

grep -rn "public-read\|public-read-write" --include=*.php .

2Fix

Stop sending a public ACL. If the file really must be public, make that decision in the bucket policy and the Block Public Access settings rather than per upload, and keep the bucket private otherwise.

<?php
// upload without 'ACL' => 'public-read'
$s3->putObject(['Bucket' => $bucket, 'Key' => $key, 'SourceFile' => $path]);

3Verify

The upload succeeds and the object stays private.

php upload.php

Confirmed by docs.aws.amazon.com

Did this fix it?

Cause 4 of 4

The client has no region or no credentials

1Check

Every SDK client needs a credential provider and a Region, set in the constructor or outside it. If no provider is named, the error comes from the last provider in the chain and can be unhelpful.

grep -n "new S3Client\|'region'\|'credentials'" upload.php

2Fix

Set the region explicitly. To get a clear error, name the provider you intend to use, for example the profile from the shared credentials file.

<?php
$s3 = new Aws\S3\S3Client([
    'region'  => 'us-west-2',
    'profile' => 'default',
]);

3Verify

A simple call such as listBuckets() works with the same client.

php -r 'require "vendor/autoload.php"; $s=new Aws\S3\S3Client(["region"=>"us-west-2","profile"=>"default"]); print_r(array_column($s->listBuckets()["Buckets"],"Name"));'

Confirmed by docs.aws.amazon.com

Did this fix it?

How do I stop it coming back?

  • Keep ACLs disabled on new buckets and grant access with policies.
  • Give each application its own IAM role with only the actions it needs.
  • Catch S3Exception in upload code and log the message.
  • Test uploads from the CLI with the same identity before blaming the PHP code.

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

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 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.