

#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import os
import random
import string
from qiniu import Auth, put_file, etag, urlsafe_base64_encode
def get_url(img):
# 需要填写你的 Access Key 和 Secret Key
access_key = 'key11112233'
secret_key = 'secret11123323213213'
# 要上传的空间
bucket_name = 'sreink'
bucket_domain = 'http://demoo1111111.qiniu.com/'
# 构建鉴权对象
q = Auth(access_key, secret_key)
# 上传到七牛后保存的文件名
ran_str = ''.join(random.sample(string.ascii_letters + string.digits, 32))
# 获取文件路径、文件名、后缀名
(filepath, tempfilename) = os.path.split(img)
(shotname, extension) = os.path.splitext(tempfilename)
key = ran_str + "/" + shotname + extension
# 生成上传 Token,可以指定过期时间等
token = q.upload_token(bucket_name, key, 3600)
# img:要上传文件的本地路径
ret, info = put_file(token, key, img)
# print(info)
assert ret['key'] == key
assert ret['hash'] == etag(img)
return bucket_domain + key
def main():
if len(sys.argv) < 2 or sys.argv[1] == "--help" or sys.argv[1] == "-h":
print "usage: upload /tmp/123.txt"
print "upload files only!"
else:
for img in sys.argv[1:]:
print img + " --> " + get_url(img)
if __name__ == '__main__':
reload(sys)
sys.setdefaultencoding('utf8')
main()