Skip to content

Instantly share code, notes, and snippets.

@garrettr
Created November 13, 2011 18:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save garrettr/1362441 to your computer and use it in GitHub Desktop.
Save garrettr/1362441 to your computer and use it in GitHub Desktop.
S3 Upload w/ Boto
def upload_to_s3(local_file, bucket_name, key_name=None, acl='private'):
'''
Uploads local_file to bucket_name on Amazon S3
key_name is the "filename" on Amazon S3. We'll default to the local file's name if
no alternative key_name is specified.
'''
# connect to Amazon S3
conn = S3Connection(AWS_ACCESS_KEY, AWS_SECRET_KEY)
bucket = conn.create_bucket(bucket_name)
k = Key(bucket)
if key_name:
k.key = key_name
else:
k.key = local_file.split("/")[-1]
# encrypt_key=True for AES-256 encryption while at rest on Amazon's servers
k.set_contents_from_filename(local_file, encrypt_key=True)
k.set_acl(acl)
debug("Uploaded %s to S3" % local_file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment