Last active
August 11, 2017 02:48
-
-
Save mugbya/2e742e484db133ee12766d65070a4f0b to your computer and use it in GitHub Desktop.
get oss policy for python3
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 datetime | |
import time | |
import base64 | |
import json | |
import hmac | |
from hashlib import sha1 as sha | |
expire_time = 30 | |
upload_dir = settings.ALIYUN_OSS_DIRECTORY_PREFIX | |
def get_iso_8601(expire): | |
gmt = datetime.datetime.fromtimestamp(expire).isoformat() | |
gmt += 'Z' | |
return gmt | |
def get_token(): | |
now = int(time.time()) | |
expire_syncpoint = now + expire_time | |
expire = get_iso_8601(expire_syncpoint) | |
policy_dict = {} | |
policy_dict['expiration'] = expire | |
condition_array = [] | |
array_item = [] | |
array_item.append('starts-with') | |
array_item.append('$key') | |
array_item.append(upload_dir) | |
condition_array.append(array_item) | |
policy_dict['conditions'] = condition_array | |
policy = json.dumps(policy_dict).strip() | |
policy_encode = base64.b64encode(policy.encode()) | |
h = hmac.new(settings.ACCESS_KEY_SECRET.encode(), policy_encode, sha) | |
sign_result = base64.b64encode(h.digest()).strip() | |
token_dict = {} | |
token_dict['accessid'] = settings.ACCESS_KEY_ID | |
token_dict['host'] = settings.ALIYUN_OSS_CDN_URL | |
token_dict['policy'] = policy_encode.decode("utf-8") | |
token_dict['signature'] = sign_result.decode("utf-8") | |
token_dict['expire'] = expire_syncpoint | |
token_dict['dir'] = upload_dir | |
result = json.dumps(token_dict) | |
return result | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment