Skip to content

Instantly share code, notes, and snippets.

@botzill
Created July 17, 2015 21:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save botzill/059cd690b376011d805a to your computer and use it in GitHub Desktop.
Save botzill/059cd690b376011d805a to your computer and use it in GitHub Desktop.
Generate signed request for amazon s3 upload
#!/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