-
-
Save botzill/059cd690b376011d805a to your computer and use it in GitHub Desktop.
Generate signed request for amazon s3 upload
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
from boto.s3.connection import S3Connection | |
AWS_ACCESS_KEY_ID = '<your key id here>' | |
AWS_SECRET_ACCESS_KEY = '<your access key here>' | |
def sign(bucket_name, key_name=None, expires_in=None, size=None): | |
conditions = [ | |
'["starts-with", "$Content-Type", ""]' | |
] | |
if not key_name: | |
key_name = '${filename}' | |
elif '${filename}' not in key_name: | |
key_name += '/${filename}' | |
if not size: | |
size = 1 * 1000 * 1000 * 100 # 100M | |
if not expires_in: | |
expires_in = 1 * 60 * 60 * 24 * 365 * 2 # 2 years | |
conn = S3Connection(AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY) | |
p = conn.build_post_form_args(bucket_name, key_name, acl='bucket-owner-full-control', | |
max_content_length=size, conditions=conditions, | |
http_method='http', expires_in=expires_in) | |
params = {} | |
for field in p['fields']: | |
params[field['name']] = field['value'] | |
params['url'] = p['action'] | |
return json.dumps(params) | |
if __name__ == '__main__': | |
bucket_name = 'pydocx' | |
key_name = 'pydocx' | |
data = sign(bucket_name, key_name) | |
print data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment