S3 / Client / put_bucket_policy

put_bucket_policy#

S3.Client.put_bucket_policy(**kwargs)#

Applies an Amazon S3 bucket policy to an Amazon S3 bucket. If you are using an identity other than the root user of the Amazon Web Services account that owns the bucket, the calling identity must have the PutBucketPolicy permissions on the specified bucket and belong to the bucket owner’s account in order to use this operation.

If you don’t have PutBucketPolicy permissions, Amazon S3 returns a 403 Access Denied error. If you have the correct permissions, but you’re not using an identity that belongs to the bucket owner’s account, Amazon S3 returns a 405 Method Not Allowed error.

Warning

To ensure that bucket owners don’t inadvertently lock themselves out of their own buckets, the root principal in a bucket owner’s Amazon Web Services account can perform the GetBucketPolicy, PutBucketPolicy, and DeleteBucketPolicy API actions, even if their bucket policy explicitly denies the root principal’s access. Bucket owner root principals can only be blocked from performing these API actions by VPC endpoint policies and Amazon Web Services Organizations policies.

For more information, see Bucket policy examples.

The following operations are related to PutBucketPolicy:

See also: AWS API Documentation

Request Syntax

response = client.put_bucket_policy(
    Bucket='string',
    ChecksumAlgorithm='CRC32'|'CRC32C'|'SHA1'|'SHA256',
    ConfirmRemoveSelfBucketAccess=True|False,
    Policy='string',
    ExpectedBucketOwner='string'
)
Parameters:
  • Bucket (string) –

    [REQUIRED]

    The name of the bucket.

  • ChecksumAlgorithm (string) –

    Indicates the algorithm used to create the checksum for the object when using the SDK. This header will not provide any additional functionality if not using the SDK. When sending this header, there must be a corresponding x-amz-checksum or x-amz-trailer header sent. Otherwise, Amazon S3 fails the request with the HTTP status code 400 Bad Request. For more information, see Checking object integrity in the Amazon S3 User Guide.

    If you provide an individual checksum, Amazon S3 ignores any provided ChecksumAlgorithm parameter.

  • ConfirmRemoveSelfBucketAccess (boolean) – Set this parameter to true to confirm that you want to remove your permissions to change this bucket policy in the future.

  • Policy (string) –

    [REQUIRED]

    The bucket policy as a JSON document.

  • ExpectedBucketOwner (string) – The account ID of the expected bucket owner. If the bucket is owned by a different account, the request fails with the HTTP status code 403 Forbidden (access denied).

Returns:

None

Examples

The following example sets a permission policy on a bucket.

response = client.put_bucket_policy(
    Bucket='examplebucket',
    Policy='{"Version": "2012-10-17", "Statement": [{ "Sid": "id-1","Effect": "Allow","Principal": {"AWS": "arn:aws:iam::123456789012:root"}, "Action": [ "s3:PutObject","s3:PutObjectAcl"], "Resource": ["arn:aws:s3:::acl3/*" ] } ]}',
)

print(response)

Expected Output:

{
    'ResponseMetadata': {
        '...': '...',
    },
}