Updating S3 CORS With Boto
Sometimes you don’t have access to the S3 console, but you do have keys for a bucket. If you need to change CORS for that bucket, it turns out you can. Boto has API methods for this.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# -*- coding: utf-8 -*- from boto.s3.connection import S3Connection from boto.s3.bucket import Bucket AWS_ACCESS_KEY = 'YOUR KEY HERE' AWS_SECRET_KEY = 'YOUR KEY HERE' S3_BUCKET = 'YOUR BUCKET HERE' cors_xml = """ <CORSConfiguration> <CORSRule> <AllowedOrigin>*</AllowedOrigin> <AllowedMethod>GET</AllowedMethod> <AllowedMethod>POST</AllowedMethod> <AllowedHeader>*</AllowedHeader> </CORSRule> </CORSConfiguration> """ connection = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY) bucket = Bucket(connection, S3_BUCKET) print "Current CORS:" print bucket.get_cors_xml() bucket.set_cors_xml(cors_xml) print "New CORS:" print bucket.get_cors_xml() |