使用 Amazon S3 存储桶策略AmazonSDK for PHP - Amazon适用于 PHP 的开发工具包
Amazon Web Services 文档中描述的 Amazon Web Services 服务或功能可能因区域而异。要查看适用于中国区域的差异,请参阅中国的 Amazon Web Services 服务入门

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

使用 Amazon S3 存储桶策略AmazonSDK for PHP

您可以使用存储桶策略来授予对 Amazon S3 资源的权限。有关更多信息,请参阅使用存储桶策略和用户策略

以下示例演示如何:

的所有示例代码Amazon适用 SDK for PHP 版本 3GitHub 上的此处.

Credentials

运行示例代码之前,请配置Amazon凭证,如中所述的凭证AmazonSDK for PHP. 然后导入AmazonSDK for PHP,如中所述的基本使用模式AmazonSDK for PHP.

获取、删除和替换存储桶策略

导入

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

示例代码

$s3Client = new S3Client([ 'profile' => 'default', 'region' => 'us-west-2', 'version' => '2006-03-01' ]); $bucket = 'my-s3-bucket'; // Get the policy of a specific bucket try { $resp = $s3Client->getBucketPolicy([ 'Bucket' => $bucket ]); echo "Succeed in receiving bucket policy:\n"; echo (string) $resp->get('Policy'); echo "\n"; } catch (AwsException $e) { // Display error message echo $e->getMessage(); echo "\n"; } // Deletes the policy from the bucket try { $resp = $s3Client->deleteBucketPolicy([ 'Bucket' => $bucket ]); echo "Succeed in deleting policy of bucket: " . $bucket . "\n"; } catch (AwsException $e) { // Display error message echo $e->getMessage(); echo "\n"; } // Replaces a policy on the bucket try { $resp = $s3Client->putBucketPolicy([ 'Bucket' => $bucket, 'Policy' => 'foo policy', ]); echo "Succeed in put a policy on bucket: " . $bucket . "\n"; } catch (AwsException $e) { // Display error message echo $e->getMessage(); echo "\n"; }