qiniu_file.py 965 B

12345678910111213141516171819202122232425262728293031
  1. from qiniu import Auth, put_file, etag,put_data
  2. import qiniu.config
  3. from django.conf import settings
  4. def upload(file_data):
  5. # 需要填写你的 Access Key 和 Secret Key
  6. access_key = settings.QINIU_ACCESS_KEY
  7. secret_key = settings.QINIU_SECRET_KEY
  8. # 构建鉴权对象
  9. q = Auth(access_key, secret_key)
  10. # 要上传的空间
  11. bucket_name = settings.BUCKET_NAME
  12. # 上传后保存的文件名
  13. # key = 'my-python-logo.png'
  14. key = None
  15. # 生成上传 Token,可以指定过期时间等
  16. token = q.upload_token(bucket_name, key, 3600)
  17. # 要上传文件的本地路径
  18. # localfile = './sync/bbb.jpg'
  19. # ret, info = put_file(token, key, localfile)
  20. # print(info)
  21. # assert ret['key'] == key
  22. # assert ret['hash'] == etag(localfile)
  23. ret, info = put_data(token,key,file_data)
  24. return ret.get("key")
  25. # with open('D:\\213.png',"rb") as f:
  26. # putile=f.read()
  27. # info = upload(putile)
  28. # print(putile)