Managing Amazon S3 bucket access permissions with the Amazon SDK for PHP Version 3 - Amazon SDK for PHP
Services or capabilities described in Amazon Web Services documentation might vary by Region. To see the differences applicable to the China Regions, see Getting Started with Amazon Web Services in China (PDF).

Managing Amazon S3 bucket access permissions with the Amazon SDK for PHP Version 3

Access control lists (ACLs) are one of the resource-based access policy options you can use to manage access to your buckets and objects. You can use ACLs to grant basic read/write permissions to other Amazon accounts. To learn more, see Managing Access with ACLs.

The following example shows how to:

  • Get the access control policy for a bucket using GetBucketAcl.

  • Set the permissions on a bucket using ACLs, using PutBucketAcl.

All the example code for the Amazon SDK for PHP is available here on GitHub.

Credentials

Before running the example code, configure your Amazon credentials, as described in Credentials. Then import the Amazon SDK for PHP, as described in Basic usage.

Get and set an access control list policy

Imports

require 'vendor/autoload.php'; use Aws\S3\S3Client; use Aws\Exception\AwsException;

Sample Code

// Create a S3Client $s3Client = new S3Client([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2006-03-01' ]); // Gets the access control policy for a bucket $bucket = 'my-s3-bucket'; try { $resp = $s3Client->getBucketAcl([ 'Bucket' => $bucket ]); echo "Succeed in retrieving bucket ACL as follows: \n"; var_dump($resp); } catch (AwsException $e) { // output error message if fails echo $e->getMessage(); echo "\n"; } // Sets the permissions on a bucket using access control lists (ACL). $params = [ 'ACL' => 'public-read', 'AccessControlPolicy' => [ // Information can be retrieved from `getBucketAcl` response 'Grants' => [ [ 'Grantee' => [ 'DisplayName' => '<string>', 'EmailAddress' => '<string>', 'ID' => '<string>', 'Type' => 'CanonicalUser', 'URI' => '<string>', ], 'Permission' => 'FULL_CONTROL', ], // ... ], 'Owner' => [ 'DisplayName' => '<string>', 'ID' => '<string>', ], ], 'Bucket' => $bucket, ]; try { $resp = $s3Client->putBucketAcl($params); echo "Succeed in setting bucket ACL.\n"; } catch (AwsException $e) { // Display error message echo $e->getMessage(); echo "\n"; }