Skip to content

Instantly share code, notes, and snippets.

@so0k
Last active October 27, 2015 04:16
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 so0k/66d5a592f803cd178b50 to your computer and use it in GitHub Desktop.
Save so0k/66d5a592f803cd178b50 to your computer and use it in GitHub Desktop.
S3 upload sample
function getS3Config() {
return Restangular.one(URLS.GET_S3_CONFIG).get();
}
//function with s3Config & angular $upload module injected
$upload.upload({
url: protocol + '://' + s3config.bucket + '/',
method: method.POST,
data: {
'key': funcs[i],
'acl': 'public-read',
'Content-Type': blob.type,
'AWSAccessKeyId': s3config.awsKey,
'success_action_status': '201',
'Policy': s3config.policy,
'Signature': s3config.signature
},
file: blob
})
import base64
import hmac, hashlib
# define view..
# define methods..
def get(request):
expiration = datetime.datetime.now() + datetime.timedelta(..)
policy = {
'expiration': expiration.strftime('%Y-%m-%dT%H:%M:%SZ'),
'conditions': [
['starts-with', '$key', ""],
{'bucket': S3_BUCKET},
{'acl': 'public-read'},
['starts-with', '$Content-Type', ""],
{'success_action_status': '201'}
]}
policy_encoded = base64.b64encode(json.dumps(policy).encode())
signature = base64.b64encode(
hmac.new(key=S3_SECRET.encode(), msg=policy_encoded, digestmod=hashlib.sha1).digest())
return Response({'policy': policy_encoded.decode(), 'signature': signature.decode(), 'bucket': S3_BUCKET, 'awsKey': S3_AWSKEY})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment