Created
March 4, 2013 02:05
-
-
Save ColtonProvias/5079405 to your computer and use it in GitHub Desktop.
S3 uploader for Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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) |
Just switch the signed variable to:
signed = b64encode(hmac.new(aws_secret.encode(), string_to_sign.encode(), sha1).digest()).decode()
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
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?