import urllib, httplib2, base64
http = httplib2.Http()
auth = base64.encodestring(username + ':' + password)
response, content = http.request(url, 'GET', headers={ 'Authorization' : 'Basic ' + auth })
Tagged with: python http urllib httplib2
Categorised as: note
# http://aws.amazon.com/articles/1434
def S3UploadParams(bucket_name, object_name, expiry, maxsize, redirect):
import os, boto, json, base64, hmac, hashlib
from time import time, gmtime, strftime
def SignS3Upload(policy_document):
policy = base64.b64encode(policy_document)
return base64.b64encode(hmac.new(
boto.config.get('Credentials', 'aws_secret_access_key'),
policy,
hashlib.sha1
).digest())
def GenerateS3PolicyString(bucket_name, object_name, expiry, maxsize, redirect):
policy_template = '{ "expiration": "%s", "conditions": [ {"bucket": "%s"}, ["eq", "$key", "%s"], {"acl": "private"}, {"success_action_redirect": "%s"}, ["content-length-range", 0, %s] ] }'
return policy_template % (
strftime("%Y-%m-%dT%H:%M:%SZ", gmtime(time() + expiry)),
bucket_name,
object_name,
redirect,
maxsize
)
params = {
'key': object_name,
'AWSAccessKeyId': boto.config.get('Credentials', 'aws_access_key_id'),
'acl': 'private',
'success_action_redirect': redirect,
}
policy = GenerateS3PolicyString(bucket_name, object_name, expiry, maxsize, redirect)
params['policy'] = base64.b64encode(policy)
signature = SignS3Upload(policy)
params['signature'] = signature
return params
Tagged with: aws django http post python s3
Categorised as: snippet