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

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

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

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

以下示例演示如何:

Amazon SDK for PHPGitHub 上提供了的所有示例代码。

凭证

运行示例代码之前,请配置您的 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"; }