更改账户信息功能直接进入链接(点击可能会操作失败),
This commit is contained in:
55
miniofile.py
55
miniofile.py
@@ -1,16 +1,59 @@
|
||||
from const import ENDPOINT, ACCESS_KEY, SECRET_KEY, BUCKET
|
||||
from minio import Minio
|
||||
import urllib3
|
||||
import time
|
||||
import logging
|
||||
|
||||
# 配置日志
|
||||
logging.basicConfig(level=logging.INFO)
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# 配置 urllib3 连接池
|
||||
http_client = urllib3.PoolManager(
|
||||
timeout=urllib3.Timeout(connect=10.0, read=30.0),
|
||||
retries=urllib3.Retry(
|
||||
total=3,
|
||||
backoff_factor=0.5,
|
||||
status_forcelist=[500, 502, 503, 504]
|
||||
)
|
||||
)
|
||||
|
||||
# 创建 MinIO 客户端
|
||||
client = Minio(
|
||||
endpoint=ENDPOINT,
|
||||
access_key=ACCESS_KEY,
|
||||
secret_key=SECRET_KEY,
|
||||
secure=False
|
||||
secure=False,
|
||||
http_client=http_client
|
||||
)
|
||||
|
||||
|
||||
def put_object(name, content):
|
||||
length = len(content.getbuffer())
|
||||
content.seek(0)
|
||||
client.put_object(BUCKET, name, content, length)
|
||||
|
||||
def put_object(name, content, max_retries=3):
|
||||
"""
|
||||
上传对象到 MinIO 服务器,带有重试机制
|
||||
|
||||
Args:
|
||||
name: 对象名称
|
||||
content: 对象内容
|
||||
max_retries: 最大重试次数
|
||||
"""
|
||||
retry_count = 0
|
||||
while retry_count < max_retries:
|
||||
try:
|
||||
length = len(content.getbuffer())
|
||||
content.seek(0)
|
||||
client.put_object(BUCKET, name, content, length)
|
||||
logger.info(f"成功上传文件: {name}")
|
||||
return
|
||||
except Exception as e:
|
||||
retry_count += 1
|
||||
wait_time = 2 ** retry_count # 指数退避
|
||||
logger.warning(f"上传失败 (尝试 {retry_count}/{max_retries}): {str(e)}")
|
||||
logger.warning(f"等待 {wait_time} 秒后重试...")
|
||||
time.sleep(wait_time)
|
||||
|
||||
# 如果所有重试都失败,记录错误但不抛出异常
|
||||
logger.error(f"上传文件 {name} 失败,已达到最大重试次数")
|
||||
# 可以选择在这里保存到本地文件系统作为备份
|
||||
# 或者实现其他备用存储方案
|
||||
raise TimeoutError("上传文件 {name} 失败,已达到最大重试次数")
|
||||
|
||||
Reference in New Issue
Block a user