| 1 |
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 |
|
| 3 |
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 |
| 4 |
International License (the "License"). You may not use this file except in compliance with the |
| 5 |
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/. |
| 6 |
|
| 7 |
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, |
| 8 |
either express or implied. See the License for the specific language governing permissions and |
| 9 |
limitations under the License. |
| 10 |
|
| 11 |
|
| 12 |
############### |
| 13 |
Bucket policies |
| 14 |
############### |
| 15 |
|
| 16 |
An S3 bucket can have an optional policy that grants access permissions to |
| 17 |
other AWS accounts or AWS Identity and Access Management (IAM) users. Bucket |
| 18 |
policies are defined using the same JSON format as a resource-based IAM policy. |
| 19 |
|
| 20 |
|
| 21 |
Retrieve a bucket policy |
| 22 |
======================== |
| 23 |
|
| 24 |
Retrieve a bucket's policy by calling the AWS SDK for Python |
| 25 |
``get_bucket_policy`` method. The method accepts a parameter that specifies |
| 26 |
the bucket name. |
| 27 |
|
| 28 |
.. code-block:: python |
| 29 |
|
| 30 |
import boto3 |
| 31 |
|
| 32 |
# Retrieve the policy of the specified bucket |
| 33 |
s3 = boto3.client('s3') |
| 34 |
result = s3.get_bucket_policy(Bucket='amzn-s3-demo-bucket') |
| 35 |
print(result['Policy']) |
| 36 |
|
| 37 |
|
| 38 |
Set a bucket policy |
| 39 |
=================== |
| 40 |
|
| 41 |
A bucket's policy can be set by calling the ``put_bucket_policy`` method. |
| 42 |
|
| 43 |
The policy is defined in the same JSON format as an IAM policy. The policy |
| 44 |
defined in the example below enables any user to retrieve any object |
| 45 |
stored in the bucket identified by the ``bucket_name`` variable. |
| 46 |
|
| 47 |
|
| 48 |
.. code-block:: python |
| 49 |
|
| 50 |
import json |
| 51 |
|
| 52 |
# Create a bucket policy |
| 53 |
bucket_name = 'amzn-s3-demo-bucket' |
| 54 |
bucket_policy = { |
| 55 |
'Version': '2012-10-17', |
| 56 |
'Statement': [{ |
| 57 |
'Sid': 'AddPerm', |
| 58 |
'Effect': 'Allow', |
| 59 |
'Principal': '*', |
| 60 |
'Action': ['s3:GetObject'], |
| 61 |
'Resource': f'arn:aws:s3:::{bucket_name}/*' |
| 62 |
}] |
| 63 |
} |
| 64 |
|
| 65 |
# Convert the policy from JSON dict to string |
| 66 |
bucket_policy = json.dumps(bucket_policy) |
| 67 |
|
| 68 |
# Set the new policy |
| 69 |
s3 = boto3.client('s3') |
| 70 |
s3.put_bucket_policy(Bucket=bucket_name, Policy=bucket_policy) |
| 71 |
|
| 72 |
|
| 73 |
Delete a bucket policy |
| 74 |
====================== |
| 75 |
|
| 76 |
A bucket's policy can be deleted by calling the ``delete_bucket_policy`` method. |
| 77 |
|
| 78 |
.. code-block:: python |
| 79 |
|
| 80 |
# Delete a bucket's policy |
| 81 |
s3 = boto3.client('s3') |
| 82 |
s3.delete_bucket_policy(Bucket='BUCKET_NAME') |