优化错误日志及重试机制
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
# https://github.com/ucloud/ufile-sdk-python
|
||||
import logging
|
||||
|
||||
from tenacity import retry, stop_after_attempt, wait_random, retry_if_exception_type
|
||||
from tenacity import retry, stop_after_attempt, wait_random, retry_if_exception_type, retry_if_result
|
||||
from ufile import filemanager
|
||||
|
||||
from ucloud import PUBLIC_KEY, PRIVATE_KEY, UPLOAD_SUFFIX, DOWNLOAD_SUFFIX, BUCKET, PRIVATE_EXPIRES, TRY_TIMES, \
|
||||
@@ -32,13 +32,13 @@ def get_private_url(key, bucket=BUCKET):
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(TRY_TIMES), wait=wait_random(MIN_WAIT_TIME, MAX_WAIT_TIME),
|
||||
retry=retry_if_exception_type(ConnectionError), reraise=True)
|
||||
retry=retry_if_result(lambda x: x is False))
|
||||
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连接失败")
|
||||
return False
|
||||
if resp.status_code != 200:
|
||||
UCLOUD_LOGGER.warning(
|
||||
f"将({source_key})从({source_bucket})拷贝到({target_bucket})失败! status: {resp.status_code} error: {resp.error}"
|
||||
@@ -48,13 +48,13 @@ def copy_file(source_bucket, source_key, target_bucket, target_key):
|
||||
|
||||
|
||||
@retry(stop=stop_after_attempt(TRY_TIMES), wait=wait_random(MIN_WAIT_TIME, MAX_WAIT_TIME),
|
||||
retry=retry_if_exception_type(ConnectionError), reraise=True)
|
||||
retry=retry_if_result(lambda x: x is False))
|
||||
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连接失败")
|
||||
return False
|
||||
if resp.status_code != 200:
|
||||
UCLOUD_LOGGER.warning(f"上传({key})失败! status: {resp.status_code} error: {resp.error}")
|
||||
return False
|
||||
|
||||
Reference in New Issue
Block a user