settings.py
ACCESS_KEY = 'aIqbHPntDHG74_SlBrrzOSEW8Vy6d6'
SECRET_KEY = 'aRslkxUvASuYVCTAHYXuIbDUt7Nvc'
#密钥 可以看这里https://portal.qiniu.com/user/key
BUCKED_NAME ='youzhu1' #创建的空间名
DOMAIN_NAME = "http://rya6r50ih.hd-bkt.clouddn.com/"
#图片地址的前缀
import os
from qiniu import Auth, put_file, etag
from django.conf import settings
import uuid
from libs.send_qinuiyun.settings import ACCESS_KEY,SECRET_KEY,BUCKED_NAME,DOMAIN_NAME
from youzhu_api.settings import dev
def qiniu_token(bucked_name,file_name): #bucked_name空间名
q = Auth(access_key=ACCESS_KEY,
secret_key=SECRET_KEY)
token = q.upload_token(bucked_name,file_name,3600)
return token
def upload_img(file_path):
"""
收集本地信息到云服务器上
参考地址:https://developer.qiniu.com/kodo/sdk/1242/python
"""
# 指定图片名称
file_name = '{}.png'.format(uuid.uuid4())
# 指定上传空间,获取token #生成上传 Token,可以指定过期时间等
token = qiniu_token(BUCKED_NAME,file_name)
ret, info = put_file(token, file_name, file_path) #file_path本地要上传的图片路径
img_url = DOMAIN_NAME + ret.get('key')
return img_url
# if __name__ == '__main__':
#
# file_path = os.path.dirname(settings.BASE_DIR)+"/media/photo/R-C.png"
# print(upload_img(file_path))
from libs.send_qinuiyun import qinui
photo_url = qinui.upload_img(photo_addr)
#photo_addr 本地图片地址
#photo_url 链接地址