django之使用七牛云、百度智能云、FastDFS上传文件(对象存储)
一、3个平台
- 七牛云:https://www.qiniu.com/
- 百度智能云:https://cloud.baidu.com/
- FastDFS:https://www.cnblogs.com/loveprogramme/p/12901787.html(需要在自己的服务器上配置)
二、文档地址
1.七牛云:https://developer.qiniu.com/kodo/sdk/1242/python
2.百度智能云:https://cloud.baidu.com/doc/BOS/s/Vjwvyrfs3
3.FasfDFS:https://www.cnblogs.com/loveprogramme/p/12901787.html
三、上传代码简要封装(函数)
1、七牛云
from qiniu import Auth, put_data, etag import qiniu.config def upload(bucket_name="images-home", key=None, data=None): # 填写accecc_key和secret_key # 换成自己的AK、SK AK = "*" SK = "*" # 构建健全对象 q = Auth(AK, SK) # 要上传的空间 # 换成自己的bucket # bucket_name = "images-home" # 上传后保存到文件名 # key = "*.png" # 生成上传 Token,可以指定过期时间等 token = q.upload_token(bucket_name, key, 3600) # 要上传文件的本地路径 # localfile = './sync/bbb.jpg' ret, info = put_data(token, key, data) print(ret, info) # ret : {'hash': 'FqoxknG28t8pQLIX4JfqDYP53Ffx', 'key': 'dj_2.jpg'} # info: _ResponseInfo__response:<Response [200]>, exception:None, status_code:200, text_body:{"hash":"FqoxknG28t8pQLIX4JfqDYP53Ffx","key":"dj_2.jpg"}, req_id:qhkAAAAZ9f4hwRAW, x_log:X-Log if info.status_code == 200:
# 返回访问文件的地址 return "http://qamsij369.bkt.clouddn.com/" + ret.get("key") else: raise Exception("文件上传失败") if __name__ == '__main__': filename = "dj_2.jpg" try: with open(filename, "rb") as fp: data = fp.read() access_url = upload(key=filename, data=data) except Exception as e: print(e) else: print(access_url)
2、百度智能云
from baidubce.bce_client_configuration import BceClientConfiguration from baidubce.auth.bce_credentials import BceCredentials from baidubce.services.bos.bos_client import BosClient def upload(key, data, bucket_name="image-home"): #设置BosClient的Host,Access Key ID和Secret Access Key bos_host = "bj.bcebos.com" access_key_id = "*" secret_access_key = "*" #创建BceClientConfiguration config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint=bos_host) # 创建上传客户端 client = BosClient(config) response = client.put_object_from_string(bucket=bucket_name, key=key, data=data) access_url = "http://" + bucket_name + "." + bos_host + "/" + key return access_url if __name__ == '__main__': filename = "dj_2.jpg" try: with open(filename, "rb") as fp: data = fp.read() access_url = upload(key=filename, data=data) except Exception as e: print(e) else: print(access_url)
3、FastDFS
def upload(buffer, file_ext_name, conf="utils/fastdfs/client.conf") from fdfs_client.client import Fdfs_client from dj32_test.settings.dev import FDFS_DOMAIN try: client = Fdfs_client(conf) except Exception as e: print("配置文件错误:", e) response = client.upload_by_buffer(buffer, file_ext_name) if "successed" in response.get("Status"): # FDFS_DOMAIn:你的fdfs文件存储访问域名 return FDFS_DOMAIN + response.get("Remote file_id") else: raise Exception("文件上传失败") if __name__ == '__main__': filename = "dj_2.jpg" try: with open(filename, "rb") as fp: data = fp.read() ext = filename.split(".")[-1] access_url = upload(data, file_ext_name=ext) except Exception as e: print(e) else: print(access_url)