1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| import datetime import uuid
from django.core.files.storage import Storage from qiniu import Auth, put_data
from settings import (QINIU_ACCESS_KEY, QINIU_SECRET_KEY, QINIU_BACKET_NAME, QINIU_URL)
class QiniuStorageObject(Storage): def __init__(self): self.now = datetime.datetime.now() self.file = None
def _new_name(self, name): new_name = "file/{0}/{1}.{2}".format( self.now.strftime("%Y/%m/%d"), str(uuid.uuid4()).replace("-", ""), name.split(".").pop(), ) return new_name
def _open(self, name, mode): return self.file
def _save(self, name, content): """ 保存文件的操作,返回值为文件的url全路径,会自动保存在需要存储的地方。 这里的content可以是一个文件对象,也可以是一个bytes对象 """ q = Auth(QINIU_ACCESS_KEY, QINIU_SECRET_KEY) token = q.upload_token(QINIU_BACKET_NAME) self.file = content file_data = content.file ret, info = put_data( token, self._new_name(name), file_data if isinstance(file_data, bytes) else file_data.read(), )
if info.status_code == 200: base_url = "%s/%s" % (QINIU_URL, ret.get("key")) return base_url else: raise Exception("上传七牛失败")
def exists(self, name): return False
def url(self, name): return name
|