upload.py
842 Bytes
# -*- coding: utf-8 -*-
# flake8: noqa
from qiniu import Auth, put_file, etag
import qiniu.config
def do_upload(access_key,secret_key,bucket_name,src_file,dst_file):
#需要填写你的 Access Key 和 Secret Key
access_key = 'BwWhoJN536BnV3CzlE20AjNKC9O2bP0l5tFpKsDU'
secret_key = '_x2VtO7fEmylgjojmLi7qwTBtRm30S8BrO0FxOPK'
#构建鉴权对象
q = Auth(access_key, secret_key)
#要上传的空间
#上传到七牛后保存的文件名
#生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, dst_file, 3600)
#要上传文件的本地路径
ret, info = put_file(token, dst_file, src_file)
try:
assert ret['key'] == dst_file
assert ret['hash'] == etag(src_file)
except Exception as e:
print e.message
return False
return True