Skip to content

Instantly share code, notes, and snippets.

@skizhak
Created January 22, 2019 21:55
Show Gist options
  • Save skizhak/004b512636df20950fc28bc8cd994962 to your computer and use it in GitHub Desktop.
Save skizhak/004b512636df20950fc28bc8cd994962 to your computer and use it in GitHub Desktop.
Following is a simple sample code that will generate the signature from your amazon credentials and add Authorization header in request to S3 server. The request to S3 service is send using linux wget command utility.
#!/usr/local/bin/python2.6
import hashlib,hmac,base64
from urlparse import urlparse
from email.utils import formatdate
import sys,subprocess
#Update credentials
AWSAccessKeyId = "<your access key>"
AWSSecretAccessKey = "<your secret key>"
url = urlparse(str(sys.argv[1]))
resource_url = '/'+url.netloc.replace(".s3.amazonaws.com", "")+url.path
amazons3time = formatdate(usegmt=True)
HTTPVerb = "GET"
ContentMD5 = ""
ContentType = ""
CanonicalizedAmzHeaders = ""
CanonicalizedResource = resource_url
string_to_sign = HTTPVerb + "\n" + ContentMD5 + "\n" + ContentType + \
"\n" + amazons3time + "\n" + CanonicalizedAmzHeaders + \
CanonicalizedResource
sig = base64.b64encode(hmac.new(AWSSecretAccessKey, \
string_to_sign, hashlib.sha1).digest())
auth_header = "Authorization:AWS "+AWSAccessKeyId+":"+sig
date_header = "Date:"+ amazons3time
cmd = "wget -S "+url.geturl()+" --header=\""+auth_header+"\" \
--header=\""+date_header+"\""
p = subprocess.Popen(cmd, shell=True, stderr=subprocess.PIPE)
while True:
outbuf = p.stderr.read(1)
if outbuf == '' and p.poll() != None:
break
if outbuf != '':
sys.stdout.write(outbuf)
sys.stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment