管理 Amazon S3 存储桶访问权限Amazon SDK for PHP版本 3 - Amazon SDK for PHP
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅 中国的 Amazon Web Services 服务入门 (PDF)

本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。

管理 Amazon S3 存储桶访问权限Amazon SDK for PHP版本 3

访问控制列表 (ACL) 是基于资源的访问策略选项之一,可用来管理对存储桶和对象的访问。您可以使用 ACL 向其他授予基本的读/写权限Amazon账户。要了解更多信息,请参阅使用 ACL 管理访问

以下示例演示如何:

的所有示例代码Amazon SDK for PHP可用此处 GitHub.

凭证

运行示例代码之前,请配置您的Amazon凭证,如中所述设置凭证. 然后导入Amazon SDK for PHP,如中所述基本用法.

获取和设置访问控制列表策略

导入

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

示例代码

// 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"; }