Updating S3 CORS With Boto

November 17, 2014

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.

# -*- 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()

Recent Posts