又拍云的签名计算方式,可以参考官网文档
本文主要介绍又拍云的表单上传,通过又拍云的表单 API 可以直接把本地文件上传到又拍云存储而不需要经过自己的服务器
代码
import hashlib import base64 import json import hashlib import datetime import requests import time import json
DEFAULT_CHUNKSIZE = 8192
bucket = '' secret = '' expiration = int(time.time())+8000 uploadfile = '/Users/wenjun/Downloads/test.mp4' save_key = '/{year}/{mon}/{day}/upload_{random32}{.suffix}' content_type = "video/mp4"
def make_content_md5(value, chunksize=DEFAULT_CHUNKSIZE): if hasattr(value, 'fileno'): md5 = hashlib.md5() for chunk in iter(lambda: value.read(chunksize), b''): md5.update(chunk) value.seek(0) return md5.hexdigest() elif isinstance(value, bytes) or (not PY3 and isinstance(value, builtin_str)): return hashlib.md5(value).hexdigest() else: raise UpYunClientException('object type error')
def make_policy(data): policy = json.dumps(data) return base64.b64encode(policy)
def upload(): data = {'bucket': bucket, 'expiration': expiration, 'save-key': save_key, 'content-type':content_type, 'b64encoded': 'on', } policy = make_policy(data) signature = make_content_md5(policy + '&' + secret) print "---------计算签名完成,准备上传参数----------------" with open(uploadfile, 'rb') as value: value = base64.b64encode(value.read()); postdata = {'policy': policy, 'signature': signature, 'file': value, } print "---------准备请求参数完成,开始上传----------------" r = requests.post("http://v0.api.upyun.com/file201503", files=postdata) if r.status_code == 200: print "---------文件上传成功,正在模拟是否可以访问----------------" uploadinfo = json.loads(r.text) headurl = "http://"+bucket+".b0.upaiyun.com"+uploadinfo["url"] print "---------模拟访问完成,以下是访问结果----------------" r = requests.head(headurl) if r.status_code == 200: print "---------文件可以访问,访问地址是:" print headurl elif requests == 404: print "---------文件访问404,由于 CDN 缓存问题,您可以稍后用浏览器尝试访问,您的地址:" print headurl
else : print "文件上传失败"
if __name__ == "__main__": upload()
|
运行结果

文章作者:阿文
版权声明:本博客所有文章除特别声明外,均采用
CC BY-NC-SA 4.0 许可协议。转载请注明来自
阿文的博客!