| 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 CORS configuration |
| 14 |
######################### |
| 15 |
|
| 16 |
Cross Origin Resource Sharing (CORS) enables client web applications in one |
| 17 |
domain to access resources in another domain. An S3 bucket can be configured |
| 18 |
to enable cross-origin requests. The configuration defines rules that specify |
| 19 |
the allowed origins, HTTP methods (GET, PUT, etc.), and other elements. |
| 20 |
|
| 21 |
|
| 22 |
Retrieve a bucket CORS configuration |
| 23 |
==================================== |
| 24 |
|
| 25 |
Retrieve a bucket's CORS configuration by calling the AWS SDK for Python |
| 26 |
``get_bucket_cors`` method. |
| 27 |
|
| 28 |
.. code-block:: python |
| 29 |
|
| 30 |
import logging |
| 31 |
import boto3 |
| 32 |
from botocore.exceptions import ClientError |
| 33 |
|
| 34 |
|
| 35 |
def get_bucket_cors(bucket_name): |
| 36 |
"""Retrieve the CORS configuration rules of an Amazon S3 bucket |
| 37 |
|
| 38 |
:param bucket_name: string |
| 39 |
:return: List of the bucket's CORS configuration rules. If no CORS |
| 40 |
configuration exists, return empty list. If error, return None. |
| 41 |
""" |
| 42 |
|
| 43 |
# Retrieve the CORS configuration |
| 44 |
s3 = boto3.client('s3') |
| 45 |
try: |
| 46 |
response = s3.get_bucket_cors(Bucket=bucket_name) |
| 47 |
except ClientError as e: |
| 48 |
if e.response['Error']['Code'] == 'NoSuchCORSConfiguration': |
| 49 |
return [] |
| 50 |
else: |
| 51 |
# AllAccessDisabled error == bucket not found |
| 52 |
logging.error(e) |
| 53 |
return None |
| 54 |
return response['CORSRules'] |
| 55 |
|
| 56 |
|
| 57 |
Set a bucket CORS configuration |
| 58 |
=============================== |
| 59 |
|
| 60 |
A bucket's CORS configuration can be set by calling the ``put_bucket_cors`` |
| 61 |
method. |
| 62 |
|
| 63 |
.. code-block:: python |
| 64 |
|
| 65 |
# Define the configuration rules |
| 66 |
cors_configuration = { |
| 67 |
'CORSRules': [{ |
| 68 |
'AllowedHeaders': ['Authorization'], |
| 69 |
'AllowedMethods': ['GET', 'PUT'], |
| 70 |
'AllowedOrigins': ['*'], |
| 71 |
'ExposeHeaders': ['ETag', 'x-amz-request-id'], |
| 72 |
'MaxAgeSeconds': 3000 |
| 73 |
}] |
| 74 |
} |
| 75 |
|
| 76 |
# Set the CORS configuration |
| 77 |
s3 = boto3.client('s3') |
| 78 |
s3.put_bucket_cors(Bucket='amzn-s3-demo-bucket', |
| 79 |
CORSConfiguration=cors_configuration) |