Sets the policy sub-resource for the specified Amazon S3 bucket. The specified policy replaces any policy the bucket already has.
To perform this operation, the caller must be authorized to set a policy for the bucket and have
PutPolicy permissions. If the caller does not have PutPolicy permissions for the bucket, Amazon S3
returns a 403 Access Denied error. If the caller has the correct permissions but has not been
authorized by the bucket owner, Amazon S3 returns a 405 Method Not Allowed error.
Access
public
Parameters
Parameter |
Type |
Required |
Description |
|---|---|---|---|
|
|
Required |
The name of the bucket to use. |
|
|
|
Required |
The JSON policy to use. |
|
|
|
Optional |
An associative array of parameters that can have the following keys:
|
Returns
Type |
Description |
|---|---|
|
A |
Examples
Set a bucket-wide policy.
See Access Policy Language for more information about writing S3 policies.
// Instantiate the class
$s3 = new AmazonS3();
$bucket = 'my-bucket-test' . strtolower($s3->key);
// Generate a new policy object
$policy = new CFPolicy($s3, array(
'Version' => '2008-10-17',
'Statement' => array(
array( // Statement #1
'Sid' => 'AddPerm',
'Effect' => 'Allow',
'Principal' => array(
'AWS' => '*'
),
'Action' => array('s3:*'),
'Resource' => array("arn:aws:s3:::${bucket}/*")
)
)
));
// Set the bucket policy
$response = $s3->set_bucket_policy($bucket, $policy);
// Success?
var_dump($response->isOK());
Result:
bool(true)
See Also
Source
Method defined in services/s3.class.php | Toggle source view (10 lines) | View on GitHub

