set_bucket_policy ( $bucket, $policy, $opt )

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

$bucket

string

Required

The name of the bucket to use.

$policy

CFPolicy

Required

The JSON policy to use.

$opt

array

Optional

An associative array of parameters that can have the following keys:

  • curlopts - array - Optional - A set of values to pass directly into curl_setopt(), where the key is a pre-defined CURLOPT_* constant.
  • returnCurlHandle - boolean - Optional - A private toggle specifying that the cURL handle be returned rather than actually completing the request. This toggle is useful for manually managed batch requests.

Returns

Type

Description

CFResponse

A CFResponse object containing a parsed HTTP response.

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)

Related Methods

See Also

Source

Method defined in services/s3.class.php | Toggle source view (10 lines) | View on GitHub

public function set_bucket_policy($bucket, CFPolicy $policy, $opt = null)
{
    if (!$opt) $opt = array();
    $opt['verb'] = 'PUT';
    $opt['sub_resource'] = 'policy';
    $opt['body'] = $policy->get_json();

    // Authenticate to S3
    return $this->authenticate($bucket, $opt);
}

Copyright © 2010–2013 Amazon Web Services, LLC


Feedback