from qiniu import Auth, put_data, put_file
class Qiniu:
""" 单例模式七牛云文件上传 """
__instance = None
def __new__(cls, **kwargs):
if not cls.__instance:
cls.__instance = super().__new__(cls)
return cls.__instance
def __init__(self, **kwargs):
# print('kwargs = ', kwargs)
need_key = ('access_key', 'secret_key', 'domain', 'bucket_name')
for key in need_key:
val = kwargs.get(key, None)
if not val:
raise ValueError('{} is necessary.'.format(key))
setattr(self, key, val)
self._auth = Auth(self.access_key, self.secret_key)
def uploadData(self, data, save_file_name):
"""
Upload data from an IO stream
:param source_file_path: 源文件路径
:param save_file_name: 保存至七牛云的文件名
:return:
"""
token = self._auth.upload_token(self.bucket_name, save_file_name, 3600)
ret, info = put_data(token, save_file_name, data)
return ret, info
def uploadFilepath(self, filepath, save_file_name):
"""
Upload a file by its path
:param source_file_path: 源文件路径
:param save_file_name: 保存至七牛云的文件名
:return:
"""
token = self._auth.upload_token(self.bucket_name, save_file_name, 3600)
# print('filepath = ', filepath)
ret, info = put_file(token, save_file_name, filepath)
return ret, info
def generateDownloadLink(self, key, timeout=None):
"""
Generate download link for a file
:key: 七牛文件名
:timeout 下载链接多久超时【秒】
:return:
"""
if timeout is None or timeout <= 0:
return self._auth.private_download_url(self.domain + key)
else:
return self._auth.private_download_url(self.domain + key, expires=timeout)
Python3 七牛云客户端模板
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- 前言 在 Python 中,访问网络资源最有名的库就是 requests、aiohttp 和 httpx。一般情况...
- 安装 py3fdfs源于fdfs-client,但在使用过程中, 和旧版略有不同.(py3fdfs官网示例有误) ...
- 从 2020 年初到现在已经开坑了快一年了。其实我最初想做一个完全模仿百度网盘的客户端程序,那时候还是 2019 ...
- 一:上传模型:http://img.blog.csdn.net/20150114183548277 二:普通客户端...