Skip to content

Instantly share code, notes, and snippets.

@ansrivas
Created April 22, 2019 12:31
Show Gist options
  • Save ansrivas/700551487eb668e92ce146b49b9a91b7 to your computer and use it in GitHub Desktop.
Save ansrivas/700551487eb668e92ce146b49b9a91b7 to your computer and use it in GitHub Desktop.
Use hmac to generate timed url
import hmac
from hashlib import sha1
import base64
import time
import urllib
s3_path = '/g4ebucket/data.tgz'
s3_access_key = 'hsjahhjj33'
s3_secret_key = 'kAJSJSDhAKJSj/kajskSAKj/='
s3_expiry = time.time() + 60 * 10 ## 10 minutes
str_to_sign = "GET\n\n\n%s\n%s" % s3_expiry,s3_path
h = hmac.new(s3_secret_key,'',hashlib.sha())
h.update(str_to_sign)
## read more about signing method: http://docs.aws.amazon.com/AmazonS3/latest/dev/RESTAuthentication.html
signature = urllib.urlencode( base64.b64encode( h.digest ) )
## print out the URL
print "http://s3.amazonaws.com%s?AWSAccessKeyId=%s&Expires=%s&Signature=%s" % s3_path, s3_access_key, s3_expiry, signature
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment