Cam Mac
Cam Mac

Reputation: 1

How can I turn off 'block public access' in AWS S3 bucket settings programmatically using AWS PHP client?

I am using the AWS PHP Client to create buckets in S3 and upload objects to them. Previously I was using ACL for making the buckets and objects publicly accessible (to the world). Using ACL with my current scripts is not working based on the AWS update last month.

I am now attempting to create buckets then apply bucket policies

Right now I can successfully create a bucket:

$result = $this->client->createBucket([
 //'ACL' => 'public-read',
 'Bucket' => $bucket,
]);

However, when I try and add a policy it will not work as the "block public access (bucket setting)" is turned on.

$result = $this->client->putBucketPolicy([
    'Bucket' => 'bucketname',
    'Policy' => '{
        "Version": "2012-10-17", 
        "Statement": [
            { 
                "Sid": "PublicReadGetObject",
                "Effect": "Allow",
                "Principal": "*", 
                "Action": "s3:GetObject", 
                "Resource": "arn:aws:s3:::bucketname/*"
            }
        ]
    }'
]);

If I turn "block public access (bucket setting)" off via web console, I'm able to add policy. But so far I have not been able to find a way to programatically turn this off. I've tried the following:

$result = $this->client->putPublicAccessBlock([
    'Bucket' => 'bucketname',
    'PublicAccessBlockConfiguration' => [
        'BlockPublicPolicy' => false,
        'BlockPublicAcls' => false,
    'IgnorePublicAcls' => false,
    'RestrictPublicBuckets' => false,
    ]
]);

But am getting the following error:

PHP Fatal error: Uncaught InvalidArgumentException: Operation not found: PutPublicAccessBlock

Ideally, it would be good to create the bucket with Block Public Access turned off, then apply the policy.

Any help here would be appreciated, I'm going around in circles.

Upvotes: 0

Views: 738

Answers (1)

Cam Mac
Cam Mac

Reputation: 1

Thanks @jarmod. That was a good steer. The SDK was indeed out of date. I updated this and was able to use putPublicAccessBlock

Upvotes: 0

Related Questions