Skip to content

Commit 7fbddc0

Browse files
Bennett Buchananrahulreddy
Bennett Buchanan
authored andcommittedMar 5, 2018
FT: Add python request example
1 parent 6d708d5 commit 7fbddc0

File tree

1 file changed

+90
-0
lines changed

1 file changed

+90
-0
lines changed
 

‎examples/python-V4Auth-example.py

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
import sys, os, base64, datetime, hashlib, hmac, datetime, calendar, json
2+
import requests # pip install requests
3+
4+
access_key = '9EQTVVVCLSSG6QBMNKO5'
5+
secret_key = 'T5mK/skkkwJ/mTjXZnHyZ5UzgGIN=k9nl4dyTmDH'
6+
7+
method = 'POST'
8+
service = 's3'
9+
host = 'localhost:8100'
10+
region = 'us-east-1'
11+
canonical_uri = '/buckets'
12+
canonical_querystring = 'Action=ListMetrics&Version=20160815'
13+
content_type = 'application/x-amz-json-1.0'
14+
algorithm = 'AWS4-HMAC-SHA256'
15+
16+
t = datetime.datetime.utcnow()
17+
amz_date = t.strftime('%Y%m%dT%H%M%SZ')
18+
date_stamp = t.strftime('%Y%m%d')
19+
20+
# Key derivation functions. See:
21+
# http://docs.aws.amazon.com/general/latest/gr/signature-v4-examples.html#signature-v4-examples-python
22+
def sign(key, msg):
23+
return hmac.new(key, msg.encode("utf-8"), hashlib.sha256).digest()
24+
25+
def getSignatureKey(key, date_stamp, regionName, serviceName):
26+
kDate = sign(('AWS4' + key).encode('utf-8'), date_stamp)
27+
kRegion = sign(kDate, regionName)
28+
kService = sign(kRegion, serviceName)
29+
kSigning = sign(kService, 'aws4_request')
30+
return kSigning
31+
32+
def get_start_time(t):
33+
start = t.replace(minute=t.minute - t.minute % 15, second=0, microsecond=0)
34+
return calendar.timegm(start.utctimetuple()) * 1000;
35+
36+
def get_end_time(t):
37+
end = t.replace(minute=t.minute - t.minute % 15, second=0, microsecond=0)
38+
return calendar.timegm(end.utctimetuple()) * 1000 - 1;
39+
40+
start_time = get_start_time(datetime.datetime(2016, 1, 1, 0, 0, 0, 0))
41+
end_time = get_end_time(datetime.datetime(2016, 2, 1, 0, 0, 0, 0))
42+
43+
# Request parameters for listing Utapi bucket metrics--passed in a JSON block.
44+
bucketListing = {
45+
'buckets': [ 'utapi-test' ],
46+
'timeRange': [ start_time, end_time ],
47+
}
48+
49+
request_parameters = json.dumps(bucketListing)
50+
51+
payload_hash = hashlib.sha256(request_parameters).hexdigest()
52+
53+
canonical_headers = \
54+
'content-type:{0}\nhost:{1}\nx-amz-content-sha256:{2}\nx-amz-date:{3}\n' \
55+
.format(content_type, host, payload_hash, amz_date)
56+
57+
signed_headers = 'content-type;host;x-amz-content-sha256;x-amz-date'
58+
59+
canonical_request = '{0}\n{1}\n{2}\n{3}\n{4}\n{5}' \
60+
.format(method, canonical_uri, canonical_querystring, canonical_headers,
61+
signed_headers, payload_hash)
62+
63+
credential_scope = '{0}/{1}/{2}/aws4_request' \
64+
.format(date_stamp, region, service)
65+
66+
string_to_sign = '{0}\n{1}\n{2}\n{3}' \
67+
.format(algorithm, amz_date, credential_scope,
68+
hashlib.sha256(canonical_request).hexdigest())
69+
70+
signing_key = getSignatureKey(secret_key, date_stamp, region, service)
71+
72+
signature = hmac.new(signing_key, (string_to_sign).encode('utf-8'),
73+
hashlib.sha256).hexdigest()
74+
75+
authorization_header = \
76+
'{0} Credential={1}/{2}, SignedHeaders={3}, Signature={4}' \
77+
.format(algorithm, access_key, credential_scope, signed_headers, signature)
78+
79+
# The 'host' header is added automatically by the Python 'requests' library.
80+
headers = {
81+
'Content-Type': content_type,
82+
'X-Amz-Content-Sha256': payload_hash,
83+
'X-Amz-Date': amz_date,
84+
'Authorization': authorization_header
85+
}
86+
87+
endpoint = 'http://' + host + canonical_uri + '?' + canonical_querystring;
88+
89+
r = requests.post(endpoint, data=request_parameters, headers=headers)
90+
print (r.text)

0 commit comments

Comments
 (0)
Please sign in to comment.