Note that there are some explanatory texts on larger screens.

plurals
  1. POPython Generated Signature for S3 Post
    text
    copied!<p>I think I've read nearly everything there is to read on base-64 encoding of a signature for in-browser, form-based post to S3: old docs and new docs. For instance:</p> <p><a href="http://doc.s3.amazonaws.com/proposals/post.html" rel="noreferrer">http://doc.s3.amazonaws.com/proposals/post.html</a></p> <p>And even found this:</p> <p><a href="http://s3.amazonaws.com/doc/s3-example-code/post/post_sample.html" rel="noreferrer">http://s3.amazonaws.com/doc/s3-example-code/post/post_sample.html</a></p> <p>Rather than using the above or Amazon's newer policy generator, or fiddle around with Boto, I'm trying to draft a simpler .py script that pulls the policy JSON from a plaintext file (policy.txt), and then generates the necessary base-64 encoded signature to help me draft the HTML form. </p> <p>The signature itself (which is reliant on the encoded policy) is NOT being encoded correctly...maybe due to some sort of utf-8 vs. ascii or \n (newline) issue?</p> <p>The script I'm working with is below, the policy and the AWS Secret Key <code>private_key</code> are from an AWS test case I'm using to see if this script works. The correctly encoded signature--as quoted by Amazon--is included in the script below for reference.</p> <p>Can anyone tell me why the signature as calculated below does not match the reference signature provided by Amazon?:</p> <p>In other words: </p> <p>Why this is correctly encoded:</p> <pre><code>policy_encoded = base64.b64encode(policy) </code></pre> <p>but this one is NOT:</p> <pre><code>signature = base64.b64encode(hmac.new(private_key, policy_encoded, sha).digest()) </code></pre> <p>PYTHON signature calculator...</p> <pre><code>#!/usr/bin/env python # -*- coding: utf-8 -*- import base64, hmac, sha from sys import argv script, policy = argv private_key = 'uV3F3YluFJax1cknvbcGwgjvx4QpvB+leU8dUj2o' input = open("..Desktop/policy.txt", "rb") policy = input.read() policy_encoded = base64.b64encode(policy) signature = base64.b64encode(hmac.new(private_key, policy_encoded, sha).digest()) print "Your policy base-64 encoded is %s." % (policy_encoded) print "Your signature base-64 encoded is %s." % (signature) print "Your signature encoded should be 2qCp0odXe7A9IYyUVqn0w2adtCA=" </code></pre> <p>JSON Policy (policy.txt--UTF-8)</p> <pre><code>{ "expiration": "2007-12-01T12:00:00.000Z", "conditions": [ {"bucket": "johnsmith"}, ["starts-with", "$key", "user/eric/"], {"acl": "public-read"}, {"success_action_redirect": "http://johnsmith.s3.amazonaws.com/successful_upload.html"}, ["starts-with", "$Content-Type", "image/"], {"x-amz-meta-uuid": "14365123651274"}, ["starts-with", "$x-amz-meta-tag", ""] ] } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload