优化ucloud的日志及重试机制
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
# 公钥
|
||||
from ufile import config
|
||||
|
||||
# 公钥
|
||||
PUBLIC_KEY = "4Z7QYI7qml36QRjcCjKrls7aHl1R6H6uq"
|
||||
# 私钥
|
||||
PRIVATE_KEY = "FIdW1Kev1Ge3K7GHXzSLyGG1wTnaG6LE9BxmIVubcCaG"
|
||||
@@ -13,5 +13,12 @@ DOWNLOAD_SUFFIX = ".cn-sh2.ufileos.com"
|
||||
# 私空间文件地址过期时间(秒)
|
||||
PRIVATE_EXPIRES = 3600
|
||||
|
||||
# 设置默认请求超时时间
|
||||
# 设置默认请求超时时间(秒)
|
||||
config.set_default(connection_timeout=60)
|
||||
|
||||
# 尝试次数
|
||||
TRY_TIMES = 5
|
||||
# 最小等待时间(秒)
|
||||
MIN_WAIT_TIME = 1
|
||||
# 最大等待时间(秒)
|
||||
MAX_WAIT_TIME = 3
|
||||
|
||||
@@ -1,58 +0,0 @@
|
||||
# https://github.com/ucloud/ufile-sdk-python
|
||||
import logging
|
||||
from time import sleep
|
||||
|
||||
from ufile import filemanager
|
||||
|
||||
from ucloud import PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX, BUCKET, PRIVATE_EXPIRES
|
||||
|
||||
UFILE_HANDLER = filemanager.FileManager(PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX)
|
||||
|
||||
|
||||
def get_private_url(key, bucket=BUCKET):
|
||||
for i in range(3):
|
||||
# 判断文件是否存在
|
||||
_, resp = UFILE_HANDLER.head_file(bucket, key)
|
||||
if resp.status_code == -1:
|
||||
logging.warning(f"uCloud连接失败!即将重试...")
|
||||
sleep(3)
|
||||
continue
|
||||
if resp.status_code != 200:
|
||||
logging.warning(f"uCloud中未找到({key})! status: {resp.status_code} error: {resp.error}")
|
||||
return None
|
||||
|
||||
# 获取公有空间下载url
|
||||
# url = get_ufile_handler.public_download_url(bucket, key)
|
||||
|
||||
# 获取私有空间下载url, expires为下载链接有效期,单位为秒
|
||||
url = UFILE_HANDLER.private_download_url(bucket, key, expires=PRIVATE_EXPIRES)
|
||||
return url
|
||||
|
||||
|
||||
def copy_file(source_bucket, source_key, target_bucket, target_key):
|
||||
for i in range(3):
|
||||
# 拷贝文件
|
||||
ret, resp = UFILE_HANDLER.copy(target_bucket, target_key, source_bucket, source_key)
|
||||
if resp.status_code == -1:
|
||||
logging.warning(f"uCloud连接失败!即将重试...")
|
||||
sleep(3)
|
||||
continue
|
||||
if resp.status_code != 200:
|
||||
logging.warning(
|
||||
f"将({source_key})从({source_bucket})拷贝到({target_bucket})失败! status: {resp.status_code} error: {resp.error}")
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
def upload_file(key, file_path, bucket=BUCKET):
|
||||
for i in range(3):
|
||||
# 普通上传文件至空间
|
||||
ret, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None)
|
||||
if resp.status_code == -1:
|
||||
logging.warning(f"uCloud连接失败!即将重试...")
|
||||
sleep(3)
|
||||
continue
|
||||
if resp.status_code != 200:
|
||||
logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}")
|
||||
return False
|
||||
return True
|
||||
61
ucloud/ufile.py
Normal file
61
ucloud/ufile.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# https://github.com/ucloud/ufile-sdk-python
|
||||
import logging
|
||||
|
||||
from tenacity import retry, stop_after_attempt, wait_random, retry_if_exception_type
|
||||
from ufile import filemanager
|
||||
|
||||
from ucloud import PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX, BUCKET, PRIVATE_EXPIRES, TRY_TIMES, \
|
||||
MIN_WAIT_TIME, MAX_WAIT_TIME
|
||||
|
||||
UFILE_HANDLER = filemanager.FileManager(PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX)
|
||||
UCLOUD_LOGGER = logging.getLogger('ucloud')
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(TRY_TIMES), wait=wait_random(MIN_WAIT_TIME, MAX_WAIT_TIME),
|
||||
retry=retry_if_exception_type(ConnectionError), reraise=True)
|
||||
def get_private_url(key, bucket=BUCKET):
|
||||
# 判断文件是否存在
|
||||
_, resp = UFILE_HANDLER.head_file(bucket, key)
|
||||
if resp.status_code == -1:
|
||||
UCLOUD_LOGGER.warning(f"查询{key}时uCloud连接失败!")
|
||||
raise ConnectionError("uCloud连接失败")
|
||||
if resp.status_code != 200:
|
||||
UCLOUD_LOGGER.warning(f"({bucket})中未找到({key})! status: {resp.status_code} error: {resp.error}")
|
||||
return None
|
||||
|
||||
# 获取公有空间下载url
|
||||
# url = get_ufile_handler.public_download_url(bucket, key)
|
||||
|
||||
# 获取私有空间下载url, expires为下载链接有效期,单位为秒
|
||||
url = UFILE_HANDLER.private_download_url(bucket, key, expires=PRIVATE_EXPIRES)
|
||||
return url
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(TRY_TIMES), wait=wait_random(MIN_WAIT_TIME, MAX_WAIT_TIME),
|
||||
retry=retry_if_exception_type(ConnectionError), reraise=True)
|
||||
def copy_file(source_bucket, source_key, target_bucket, target_key):
|
||||
# 复制文件
|
||||
_, resp = UFILE_HANDLER.copy(target_bucket, target_key, source_bucket, source_key)
|
||||
if resp.status_code == -1:
|
||||
UCLOUD_LOGGER.warning(f"复制{source_key}时uCloud连接失败!")
|
||||
raise ConnectionError("uCloud连接失败")
|
||||
if resp.status_code != 200:
|
||||
UCLOUD_LOGGER.warning(
|
||||
f"将({source_key})从({source_bucket})拷贝到({target_bucket})失败! status: {resp.status_code} error: {resp.error}"
|
||||
)
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(TRY_TIMES), wait=wait_random(MIN_WAIT_TIME, MAX_WAIT_TIME),
|
||||
retry=retry_if_exception_type(ConnectionError), reraise=True)
|
||||
def upload_file(key, file_path, bucket=BUCKET):
|
||||
# 普通上传文件至云空间
|
||||
_, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None)
|
||||
if resp.status_code == -1:
|
||||
UCLOUD_LOGGER.warning(f"上传{key}时uCloud连接失败!即将重试...")
|
||||
raise ConnectionError("uCloud连接失败")
|
||||
if resp.status_code != 200:
|
||||
UCLOUD_LOGGER.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}")
|
||||
return False
|
||||
return True
|
||||
Reference in New Issue
Block a user