Last active
October 27, 2015 04:16
-
-
Save so0k/66d5a592f803cd178b50 to your computer and use it in GitHub Desktop.
S3 upload sample
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
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 | |
}) |
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
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