This Python example shows you how to configure the cross-origin resource sharing (CORS) permissions for a bucket.
In this example, Python code is used to list your Amazon S3 buckets and to configure CORS and bucket logging. The Python code uses the AWS SDK for Python to configure a selected Amazon S3 bucket using these methods of the Amazon S3 client class:
For more information about using CORS configuration with an Amazon S3 bucket, see Cross-Origin Resource Sharing (CORS) in the Amazon Simple Storage Service Developer Guide.
All the example code for the Amazon Web Services (AWS) SDK for Python is available here on GitHub.
To set up and run this example, you must first complete this task:
The example below shows how to:
import boto3
# Create an S3 client
s3 = boto3.client('s3')
# Call S3 to get CORS configuration for selected bucket
result = s3.get_bucket_cors(Bucket='my-bucket')
The example below shows how to:
import boto3
# Create an S3 client
s3 = boto3.client('s3')
# Create the CORS configuration
cors_configuration = {
'CORSRules': [{
'AllowedHeaders': ['Authorization'],
'AllowedMethods': ['GET', 'PUT'],
'AllowedOrigins': ['*'],
'ExposeHeaders': ['GET', 'PUT'],
'MaxAgeSeconds': 3000
}]
}
# Set the new CORS configuration on the selected bucket
s3.put_bucket_cors(Bucket='my-bucket', CORSConfiguration=cors_configuration)