新增ucloud的请求失败的重试功能

This commit is contained in:
2024-07-19 16:34:11 +08:00
parent 60c61f318a
commit 8f2ce3f6e0
2 changed files with 44 additions and 23 deletions

View File

@@ -1,4 +1,6 @@
# 公钥 # 公钥
from ufile import config
PUBLIC_KEY = "4Z7QYI7qml36QRjcCjKrls7aHl1R6H6uq" PUBLIC_KEY = "4Z7QYI7qml36QRjcCjKrls7aHl1R6H6uq"
# 私钥 # 私钥
PRIVATE_KEY = "FIdW1Kev1Ge3K7GHXzSLyGG1wTnaG6LE9BxmIVubcCaG" PRIVATE_KEY = "FIdW1Kev1Ge3K7GHXzSLyGG1wTnaG6LE9BxmIVubcCaG"
@@ -10,3 +12,6 @@ UPLOAD_SUFFIX = ".cn-sh2.ufileos.com"
DOWNLOAD_SUFFIX = ".cn-sh2.ufileos.com" DOWNLOAD_SUFFIX = ".cn-sh2.ufileos.com"
# 私空间文件地址过期时间(秒) # 私空间文件地址过期时间(秒)
PRIVATE_EXPIRES = 3600 PRIVATE_EXPIRES = 3600
# 设置默认请求超时时间
config.set_default(connection_timeout=60)

View File

@@ -1,5 +1,6 @@
# https://github.com/ucloud/ufile-sdk-python # https://github.com/ucloud/ufile-sdk-python
import logging import logging
from time import sleep
from ufile import filemanager from ufile import filemanager
@@ -9,8 +10,13 @@ UFILE_HANDLER = filemanager.FileManager(PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX,
def get_private_url(key, bucket=BUCKET): def get_private_url(key, bucket=BUCKET):
for i in range(3):
# 判断文件是否存在 # 判断文件是否存在
_, resp = UFILE_HANDLER.head_file(bucket, key) _, resp = UFILE_HANDLER.head_file(bucket, key)
if resp.status_code == -1:
logging.warning(f"uCloud连接失败!即将重试...")
sleep(1)
continue
if resp.status_code != 200: if resp.status_code != 200:
logging.warning(f"uCloud中未找到({key})! status: {resp.status_code} error: {resp.error}") logging.warning(f"uCloud中未找到({key})! status: {resp.status_code} error: {resp.error}")
return None return None
@@ -24,8 +30,13 @@ def get_private_url(key, bucket=BUCKET):
def copy_file(source_bucket, source_key, target_bucket, target_key): 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) ret, resp = UFILE_HANDLER.copy(target_bucket, target_key, source_bucket, source_key)
if resp.status_code == -1:
logging.warning(f"uCloud连接失败!即将重试...")
sleep(1)
continue
if resp.status_code != 200: if resp.status_code != 200:
logging.warning( logging.warning(
f"将({source_key})从({source_bucket})拷贝到({target_bucket})失败! status: {resp.status_code} error: {resp.error}") f"将({source_key})从({source_bucket})拷贝到({target_bucket})失败! status: {resp.status_code} error: {resp.error}")
@@ -34,8 +45,13 @@ def copy_file(source_bucket, source_key, target_bucket, target_key):
def upload_file(key, file_path, bucket=BUCKET): def upload_file(key, file_path, bucket=BUCKET):
for i in range(3):
# 普通上传文件至空间 # 普通上传文件至空间
ret, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None) ret, resp = UFILE_HANDLER.putfile(bucket, key, file_path, header=None)
if resp.status_code == -1:
logging.warning(f"uCloud连接失败!即将重试...")
sleep(1)
continue
if resp.status_code != 200: if resp.status_code != 200:
logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}") logging.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}")
return False return False