Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
S3 uploader for Python 3
"""
S3 Uploader for Python 3
Colton J. Provias
Usage:
f = open('sample.png', 'rb')
contents = f.read()
response, url = upload_to_s3('AWSKEY', 'AWSSECRET', 'mybucket', 'image.png', contents, 'image/png')
"""
from _sha1 import sha1
from base64 import b64encode
from datetime import datetime
import hmac
from http.client import HTTPConnection
from wsgiref.handlers import format_date_time
def upload_to_s3(aws_key, aws_secret, bucket, filename, contents, mimetype):
timestamp = format_date_time(datetime.now().timestamp())
string_to_sign = '\n'.join(['PUT', '', mimetype, timestamp, 'x-amz-acl:public-read', '/' + bucket + '/' + filename])
signed = b64encode(hmac.new(aws_secret.encode('utf-8'), string_to_sign.encode('utf-8'), sha1)).digest().decode(
'utf-8')
headers = {
'Authorization': 'AWS ' + aws_key + ':' + signed,
'Content-Type': mimetype,
'Date': timestamp,
'Content-Length': len(contents),
'x-amz-acl': 'public-read'
}
conn = HTTPConnection(bucket + '.s3.amazonaws.com')
conn.request('PUT', filename, contents, headers)
return (conn.getresponse(), 'http://' + bucket + '.s3.amazonaws.com/' + filename)
@ygbr

This comment has been minimized.

Copy link

commented Mar 22, 2013

Getting this error, every time:

File "py3s3.py", line 23, in upload_to_s3
signed = b64encode(hmac.new(aws_secret.encode('utf-8'), string_to_sign.encode('utf-8'), sha1)).digest().decode(
File "/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/lib/python3.3/base64.py", line 58, in b64encode
raise TypeError("expected bytes, not %s" % s.class.name)
TypeError: expected bytes, not HMAC

already tried to decouple HMAC from base64 encoding function and encode it pre-b64 but no success... ideas?

@ygbr

This comment has been minimized.

Copy link

commented Mar 22, 2013

Just switch the signed variable to:

signed = b64encode(hmac.new(aws_secret.encode(), string_to_sign.encode(), sha1).digest()).decode()
@ColtonProvias

This comment has been minimized.

Copy link
Owner Author

commented Oct 4, 2013

Strange. I tested it on Python 3.3.0 with no issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.