上传文件也弄个超时
This commit is contained in:
parent
358207efdc
commit
2ea248c02e
28
util/oss.py
28
util/oss.py
@ -13,14 +13,23 @@ def upload_to_oss(url, file_path):
|
|||||||
:param str file_path: 文件路径
|
:param str file_path: 文件路径
|
||||||
:return bool: 是否成功
|
:return bool: 是否成功
|
||||||
"""
|
"""
|
||||||
with open(file_path, 'rb') as f:
|
max_retries = 5
|
||||||
|
retries = 0
|
||||||
|
while retries < max_retries:
|
||||||
try:
|
try:
|
||||||
response = requests.put(url, data=f)
|
with open(file_path, 'rb') as f:
|
||||||
return response.status_code == 200
|
response = requests.put(url, data=f, timeout=60) # 设置超时时间为1分钟
|
||||||
|
if response.status_code == 200:
|
||||||
|
return True
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
retries += 1
|
||||||
|
logger.warning(f"Upload timed out. Retrying {retries}/{max_retries}...")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.warning(f"Upload failed. Retrying {retries}/{max_retries}...")
|
||||||
|
retries += 1
|
||||||
return False
|
return False
|
||||||
|
|
||||||
|
|
||||||
def download_from_oss(url, file_path):
|
def download_from_oss(url, file_path):
|
||||||
"""
|
"""
|
||||||
使用签名URL下载文件到OSS
|
使用签名URL下载文件到OSS
|
||||||
@ -33,11 +42,18 @@ def download_from_oss(url, file_path):
|
|||||||
if file_dir:
|
if file_dir:
|
||||||
if not os.path.exists(file_dir):
|
if not os.path.exists(file_dir):
|
||||||
os.makedirs(file_dir)
|
os.makedirs(file_dir)
|
||||||
|
max_retries = 5
|
||||||
|
retries = 0
|
||||||
|
while retries < max_retries:
|
||||||
try:
|
try:
|
||||||
response = requests.get(url)
|
response = requests.get(url, timeout=15) # 设置超时时间
|
||||||
with open(file_path, 'wb') as f:
|
with open(file_path, 'wb') as f:
|
||||||
f.write(response.content)
|
f.write(response.content)
|
||||||
return True
|
return True
|
||||||
|
except requests.exceptions.Timeout:
|
||||||
|
retries += 1
|
||||||
|
logger.warning(f"Download timed out. Retrying {retries}/{max_retries}...")
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(e)
|
logger.warning(f"Download failed. Retrying {retries}/{max_retries}...")
|
||||||
|
retries += 1
|
||||||
return False
|
return False
|
||||||
|
Loading…
x
Reference in New Issue
Block a user