Skip to content

Instantly share code, notes, and snippets.

@aaronlevy
Last active August 29, 2015 13:57
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 aaronlevy/9797166 to your computer and use it in GitHub Desktop.
Save aaronlevy/9797166 to your computer and use it in GitHub Desktop.
Simple S3 download (for when boto is more than you need)
import base64
import hmac
import sha
import time
import urllib2
def download(access_key, secret_key, bucket, key, destination=None, timeout=None):
url = 'http://%s.s3.amazonaws.com/%s' % (bucket, key)
headers = _auth_header('GET', bucket, key, access_key, secret_key)
data = urllib2.urlopen(
urllib2.Request(url, headers=headers),
timeout=timeout
).read()
if destination is None:
return data
with open(destination, 'wb') as local_file:
local_file.write(data)
def _auth_header(method, bucket, key, access_key, secret_key, amz_headers=None):
ss = "{method}\n\n\n\n{amz_headers}\n{resource}"
amz_headers = amz_headers or {}
date = time.strftime("%a, %d %b %Y %X GMT", time.gmtime())
amz_headers['x-amz-date'] = date
# Header in format of "key:value\nkey:value" sorted by key
header_string = '\n'.join(
[':'.join([k, amz_headers[k]]) for k in sorted(amz_headers)]
)
sign_string = ss.format(
method=method,
amz_headers=header_string,
resource='/'+bucket+'/'+key
)
auth_string = base64.encodestring(
hmac.new(
secret_key,
sign_string,
sha
).digest()).strip()
headers = {
'Authorization': 'AWS %s:%s' % (access_key, auth_string),
'X-Amz-Date': date
}
return headers
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment