Skip to content

Instantly share code, notes, and snippets.

@kirang89
Created July 28, 2014 14:55
Show Gist options
  • Save kirang89/db73e22199c587d69dcf to your computer and use it in GitHub Desktop.
Save kirang89/db73e22199c587d69dcf to your computer and use it in GitHub Desktop.
Uploading a file to Amazon S3 using boto
import cStringIO
from boto.s3.connection import S3Connection
from boto.s3.key import Key
def s3upload(filename, extension, file):
conn = S3Connection('AWS_ACCESS_KEY', 'AWS_SECRET_KEY')
k = Key(conn.get_bucket('<bucket name>'))
k.key = filename
k.set_metadata("Content-Type", file.mimetype)
fp = cStringIO.StringIO(file.read())
k.set_contents_from_file(fp)
k.set_acl("public-read")
fp.close()
return "https://s3.amazonaws.com/<bucket name>/{}.{}".format(filename, extension)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment