S3 PutObject fails in PHP with AccessDenied or AccessControlListNotSupported
An error occurred (AccessDenied) when calling the PutObject operationShort 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.
Fix it fast
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(); }2Match what you see, then jump to its fix
- 1
403 AccessDenied with the phrase because no identity-based policy allows the s3:PutObject actionThe IAM user or role has no s3:PutObject permissionFix - 2
400 with the error code AccessControlListNotSupportedThe upload sends an ACL, but the bucket has ACLs disabledFix - 3
AccessDenied with because public ACLs are prevented by the BlockPublicAcls setting in S3 Block Public AccessBlock Public Access rejects a public ACLFix - 4
A CredentialsException, or a generic credentials error from the last provider in the chainThe client has no region or no credentialsFix
- 1
What does the error look like?
An error occurred (AccessDenied) when calling the PutObject operationbecause no identity-based policy allows the s3:PutObject actionAccessControlListNotSupportedbecause 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-identity2Fix
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.txtConfirmed by docs.aws.amazon.com
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-bucket2Fix
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.phpConfirmed by docs.aws.amazon.com
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.phpConfirmed by docs.aws.amazon.com
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.php2Fix
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
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:
- Dispatch ProA real-time logistics platform: Flutter apps, Spring Boot services and a Kafka event pipeline on AWS.
- RaheebA consumer app shipped to both stores from one Flutter codebase on a .NET and Azure back end.
- Spotless LaundryAn on-demand laundry app: Flutter on a Laravel back end, with Firebase for live updates.
Related fixes
Back end
Amazon S3: The request signature we calculated does not match the signature you provided
Error SignatureDoesNotMatch, Status 403
S3 computed a different signature from the one in your request.
4 causes, each with a fixBack end
AWS SDK for PHP cannot get credentials from the instance profile metadata server
Error retrieving credentials from the instance profile metadata server.
The SDK tries the EC2 instance metadata service when it has no valid credentials from anywhere earlier in its chain.
4 causes, each with a fixBack end
AWS SDK for PHP rejects requests: the signature is from the future
Signature not yet current: {date} is still later than {date}
AWS compares the timestamp in your signed request with its own clock.
2 causes, each with a fixBack end
Laravel cannot find League\Flysystem\AwsS3v3\AwsS3Adapter
Class 'League\Flysystem\AwsS3v3\AwsS3Adapter' not found
The S3 adapter package is missing, or your code names the class from an older Flysystem.
2 causes, each with a fixBack end
Android gets HTML instead of JSON from a PHP API
org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
The response body is not JSON.
2 causes, each with a fixBack 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