python-boto3/docs/source/guide/s3-example-configuring-buckets.rst

80 lines
2.6 KiB
ReStructuredText
Raw Normal View History

2019-10-20 18:51:09 +02:00
.. Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
2018-07-11 07:39:36 +02:00
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0
International License (the "License"). You may not use this file except in compliance with the
License. A copy of the License is located at http://creativecommons.org/licenses/by-nc-sa/4.0/.
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
either express or implied. See the License for the specific language governing permissions and
limitations under the License.
2019-10-20 18:51:09 +02:00
#########################
2021-09-22 18:34:33 +02:00
Bucket CORS configuration
2019-10-20 18:51:09 +02:00
#########################
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
Cross Origin Resource Sharing (CORS) enables client web applications in one
domain to access resources in another domain. An S3 bucket can be configured
to enable cross-origin requests. The configuration defines rules that specify
the allowed origins, HTTP methods (GET, PUT, etc.), and other elements.
2018-07-11 07:39:36 +02:00
2021-09-22 18:34:33 +02:00
Retrieve a bucket CORS configuration
2019-10-20 18:51:09 +02:00
====================================
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
Retrieve a bucket's CORS configuration by calling the AWS SDK for Python
``get_bucket_cors`` method.
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
.. code-block:: python
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
import logging
import boto3
from botocore.exceptions import ClientError
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
def get_bucket_cors(bucket_name):
"""Retrieve the CORS configuration rules of an Amazon S3 bucket
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
:param bucket_name: string
:return: List of the bucket's CORS configuration rules. If no CORS
configuration exists, return empty list. If error, return None.
"""
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
# Retrieve the CORS configuration
s3 = boto3.client('s3')
try:
response = s3.get_bucket_cors(Bucket=bucket_name)
except ClientError as e:
if e.response['Error']['Code'] == 'NoSuchCORSConfiguration':
return []
else:
# AllAccessDisabled error == bucket not found
logging.error(e)
return None
return response['CORSRules']
2018-07-11 07:39:36 +02:00
2021-09-22 18:34:33 +02:00
Set a bucket CORS configuration
2018-07-11 07:39:36 +02:00
===============================
2019-10-20 18:51:09 +02:00
A bucket's CORS configuration can be set by calling the ``put_bucket_cors``
method.
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
.. code-block:: python
2018-07-11 07:39:36 +02:00
2019-10-20 18:51:09 +02:00
# Define the configuration rules
2018-07-11 07:39:36 +02:00
cors_configuration = {
'CORSRules': [{
'AllowedHeaders': ['Authorization'],
'AllowedMethods': ['GET', 'PUT'],
'AllowedOrigins': ['*'],
'ExposeHeaders': ['GET', 'PUT'],
'MaxAgeSeconds': 3000
}]
}
2019-10-20 18:51:09 +02:00
# Set the CORS configuration
s3 = boto3.client('s3')
s3.put_bucket_cors(Bucket='BUCKET_NAME',
CORSConfiguration=cors_configuration)