5fd209eb3707c8528.jpg_fo742.jpg

#!/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 = '11111111111111111111111111111-euT'
    secret_key = '2222222222222222222222'
    # 要上传的空间
    bucket_name = '333333333333'
    bucket_domain = 'http://rrrrrrrrrr.bkt.clouddn.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()

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注

Captcha Code