Skip to content

Instantly share code, notes, and snippets.

@DmZ
Created November 13, 2012 18:39
Show Gist options
  • Save DmZ/4067564 to your computer and use it in GitHub Desktop.
Save DmZ/4067564 to your computer and use it in GitHub Desktop.
Simple S3 signing for expiring URLs
#!/usr/bin/env python
import os
import sys
import hmac
import time
import urllib
import base64
import hashlib
import urlparse
expires = int(time.time() + int(sys.argv[1]))
url = urlparse.urlparse(sys.argv[2])
access_key = os.environ["AWS_ACCESS_KEY_ID"]
secret_key = os.environ["AWS_SECRET_ACCESS_KEY"]
h = hmac.new(secret_key,
"GET\n\n\n%d\n%s" % (expires, url.path),
hashlib.sha1)
sign = urllib.quote_plus(base64.encodestring(h.digest()).strip())
print("%s?AWSAccessKeyId=%s&Expires=%d&Signature=%s" % (sys.argv[2],
access_key,
expires, sign))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment