mirror of
https://github.com/fastapiadmin/FastapiAdmin.git
synced 2026-10-15 21:35:34 +00:00
refactor(backend): 错误码收敛为单一事实来源,统一异常写法
错误码(t16)
- HTTP 语义只由 core/exceptions.py 的 RET.code → fastapi.status 映射决定;
映射表显式登记 -1→400 与全部「数值即 HTTP 状态」的 RET 成员,_DEFAULT_BUSINESS_STATUS
降为「新增码忘登记」的安全网
- 移除冗余 status_code:131 处调用点改为纯 code 表达;security.py/dependencies.py
的 status_code=401/403 去除(映射表已给);get_or_404 → code=RET.NOT_FOUND.code
- 新增静态护栏(AST):app/ 下除 exceptions.py 外不得出现 status_code 传参,
路由装饰器 201 白名单显式放行并断言白名单非空
异常写法统一(t19)
- 删除 CustomException.internal 及 132 处包装:意外异常直接冒泡,由全局
@app.exception_handler(Exception) 统一映射 5xx + 通用文案 + 日志;
需要运维上下文处改为 logger.exception("<业务动作>失败") + raise
- 构造器不再接受 status_code 参数(属性保留,由 code 派生)→ 结构上杜绝第二来源
- 修复 6 处「宽泛 except 把 400 包成 500」的语义反转(补 except CustomException: raise),
并加 AST 护栏防止回潮
- 全局 SQLAlchemyError:非完整性错误由 400 改为 503(连接类)/500(其它),
不再把驱动原文/异常类型拼进客户端 msg;顺带修掉不存在的
status.HTTP_403_SERVICE_UNAVAILABLE 常量(命中即 AttributeError→500)
验证:pytest 97 passed / ruff 全绿;AST 审计残留 0;护栏红绿测试通过;
线上实测验证码过期仍 400 并保留业务文案、无令牌 401。
This commit is contained in:
@@ -76,11 +76,12 @@ def encrypt_password(plain: str | None) -> str:
|
||||
|
||||
|
||||
def decrypt_password(cipher: str | None) -> str:
|
||||
"""密文 → 明文密码。空值原样返回空串,解不开时抛业务异常。"""
|
||||
try:
|
||||
return CryptoUtil.decrypt(cipher)
|
||||
except CustomException as e:
|
||||
raise CustomException(msg=f"存储源密码解密失败:{e!s}", status_code=500)
|
||||
"""密文 → 明文密码。空值原样返回空串。
|
||||
|
||||
解密失败(密钥变更/数据损坏)由 ``CryptoUtil.decrypt`` 抛 ``RuntimeError``,
|
||||
交由全局异常处理器映射为 5xx(t19:不在业务代码里包装意外异常,细节只进日志)。
|
||||
"""
|
||||
return CryptoUtil.decrypt(cipher)
|
||||
|
||||
|
||||
# 文件系统类协议与对象存储类协议的划分:仅用于文档说明与配置元数据,
|
||||
@@ -322,7 +323,8 @@ class BaseStorageAdapter(ABC):
|
||||
with ThreadPoolExecutor(max_workers=workers) as pool:
|
||||
list(pool.map(_run, tasks))
|
||||
if errors:
|
||||
raise CustomException(msg=f"批量上传失败 {len(errors)}/{len(tasks)} 个文件,首个错误: {errors[0]!s}", status_code=500)
|
||||
logger.error(f"批量上传失败 {len(errors)}/{len(tasks)} 个文件,首个错误: {errors[0]!s}")
|
||||
raise RuntimeError(f"批量上传失败 {len(errors)}/{len(tasks)} 个文件,首个错误: {errors[0]!s}")
|
||||
return len(tasks)
|
||||
|
||||
def _sync_download_dir(self, remote_dir: str, local_dir: str, concurrency: int) -> int:
|
||||
@@ -352,7 +354,8 @@ class BaseStorageAdapter(ABC):
|
||||
with ThreadPoolExecutor(max_workers=workers) as pool:
|
||||
list(pool.map(_run, tasks))
|
||||
if errors:
|
||||
raise CustomException(msg=f"批量下载失败 {len(errors)}/{len(tasks)} 个文件,首个错误: {errors[0]!s}", status_code=500)
|
||||
logger.error(f"批量下载失败 {len(errors)}/{len(tasks)} 个文件,首个错误: {errors[0]!s}")
|
||||
raise RuntimeError(f"批量下载失败 {len(errors)}/{len(tasks)} 个文件,首个错误: {errors[0]!s}")
|
||||
return len(tasks)
|
||||
|
||||
@staticmethod
|
||||
|
||||
@@ -95,8 +95,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
MAXThread=concurrency,
|
||||
EnableMD5=self._adv.enable_md5,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 上传失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 上传失败')
|
||||
raise
|
||||
return remote_path
|
||||
|
||||
def _sync_download(self, remote_path: str, local_path: str) -> str:
|
||||
@@ -106,8 +107,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
Key=remote_path,
|
||||
DestFilePath=local_path,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 下载失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 下载失败')
|
||||
raise
|
||||
return local_path
|
||||
|
||||
def _sync_delete(self, remote_path: str) -> None:
|
||||
@@ -121,8 +123,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
"""
|
||||
try:
|
||||
self.client.delete_object(Bucket=self._require_bucket(), Key=remote_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 删除失败')
|
||||
raise
|
||||
|
||||
def _sync_exists(self, remote_path: str) -> bool:
|
||||
try:
|
||||
@@ -180,8 +183,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
else:
|
||||
break
|
||||
return result
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_page(self, prefix: str, page_size: int, cursor: str | None) -> StoragePage:
|
||||
"""游标分页:单次 SDK 请求只拉一页(COS Marker),翻页经前端回传游标。"""
|
||||
@@ -249,8 +253,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
has_next=truncated,
|
||||
next_cursor=next_cursor or None,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_buckets(self) -> list[str]:
|
||||
"""列出账号下全部存储桶。"""
|
||||
@@ -258,8 +263,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
resp = self.client.list_buckets()
|
||||
buckets = (resp.get("Buckets") or {}).get("Bucket") or []
|
||||
return [b.get("Name", "") for b in buckets if b.get("Name")]
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 桶列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 桶列表失败')
|
||||
raise
|
||||
|
||||
def _sync_get_url(self, remote_path: str, expire: int) -> str:
|
||||
"""生成预签名 URL
|
||||
@@ -279,8 +285,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
Key=remote_path,
|
||||
Expired=expire,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 生成预签名 URL 失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 生成预签名 URL 失败')
|
||||
raise
|
||||
|
||||
# ── 目录操作(对象存储以 key/ 占位对象模拟目录)──────────────────
|
||||
|
||||
@@ -310,14 +317,16 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
def _sync_mkdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self.client.put_object(Bucket=self._require_bucket(), Key=remote_dir.rstrip("/") + "/", Body=b"")
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 创建目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 创建目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rmdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self.client.delete_object(Bucket=self._require_bucket(), Key=remote_dir.rstrip("/") + "/")
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 删除目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 删除目录失败')
|
||||
raise
|
||||
|
||||
def _sync_copy(self, src: str, dst: str) -> None:
|
||||
"""复制:目录走基类递归实现;文件用服务端 copy_object。"""
|
||||
@@ -330,8 +339,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
Key=dst,
|
||||
CopySource={"Bucket": self._require_bucket(), "Key": src},
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 复制失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 复制失败')
|
||||
raise
|
||||
|
||||
def _sync_rename(self, src: str, dst: str) -> None:
|
||||
"""重命名/移动:目录先复制后删除;文件 copy_object 后删源。"""
|
||||
@@ -344,8 +354,9 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
def _sync_list_recursive(self, prefix: str) -> list[StorageObject]:
|
||||
try:
|
||||
keys = self._list_all_keys(prefix)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 递归列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 递归列表失败')
|
||||
raise
|
||||
return self._entries_from_keys(keys)
|
||||
|
||||
def _sync_delete_dir(self, remote_dir: str) -> None:
|
||||
@@ -360,5 +371,6 @@ class CosStorageAdapter(BaseStorageAdapter):
|
||||
Bucket=self._require_bucket(),
|
||||
Delete={"Objects": [{"Key": k} for k in keys[i : i + 1000]], "Quiet": True},
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"COS 递归删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('COS 递归删除失败')
|
||||
raise
|
||||
|
||||
@@ -5,7 +5,6 @@ import ssl
|
||||
import tempfile
|
||||
from datetime import datetime
|
||||
|
||||
from app.core.exceptions import CustomException
|
||||
from app.core.logger import logger
|
||||
from app.modules.task.storage.core.base import BaseStorageAdapter, FtpAdvancedConfig, StorageObject, StorageProtocol
|
||||
|
||||
@@ -100,8 +99,9 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
self._ensure_remote_dir(client, dir_part)
|
||||
with open(local_path, "rb") as f:
|
||||
client.storbinary(f"STOR {remote_path}", f)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 上传失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 上传失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
return remote_path
|
||||
@@ -122,8 +122,9 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
try:
|
||||
with open(local_path, "wb") as f:
|
||||
client.retrbinary(f"RETR {remote_path}", f.write)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 下载失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 下载失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
return local_path
|
||||
@@ -132,8 +133,9 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
client = self._new_client()
|
||||
try:
|
||||
client.delete(remote_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 删除失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
|
||||
@@ -181,8 +183,9 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
)
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 列表失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
|
||||
@@ -199,8 +202,9 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
client.mkd(current)
|
||||
except ftplib.error_perm:
|
||||
pass # 目录已存在时跳过(部分服务器返回 550)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 创建目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 创建目录失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
|
||||
@@ -208,8 +212,9 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
client = self._new_client()
|
||||
try:
|
||||
client.rmd(remote_dir)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 删除目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 删除目录失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
|
||||
@@ -217,8 +222,9 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
client = self._new_client()
|
||||
try:
|
||||
client.rename(src, dst)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 重命名失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 重命名失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
|
||||
@@ -247,7 +253,8 @@ class FtpStorageAdapter(BaseStorageAdapter):
|
||||
client.storbinary(f"STOR {dst}", f)
|
||||
finally:
|
||||
os.remove(tmp_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"FTP 复制失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('FTP 复制失败')
|
||||
raise
|
||||
finally:
|
||||
self._close_client(client)
|
||||
|
||||
@@ -36,16 +36,18 @@ class LocalStorageAdapter(BaseStorageAdapter):
|
||||
try:
|
||||
os.makedirs(os.path.dirname(dest), exist_ok=True)
|
||||
shutil.copy2(local_path, dest)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"本地存储上传失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('本地存储上传失败')
|
||||
raise
|
||||
return remote_path
|
||||
|
||||
def _sync_download(self, remote_path: str, local_path: str) -> str:
|
||||
src = self._abs_path(remote_path)
|
||||
try:
|
||||
shutil.copy2(src, local_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"本地存储下载失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('本地存储下载失败')
|
||||
raise
|
||||
return local_path
|
||||
|
||||
def _sync_delete(self, remote_path: str) -> None:
|
||||
@@ -55,8 +57,9 @@ class LocalStorageAdapter(BaseStorageAdapter):
|
||||
shutil.rmtree(target)
|
||||
elif os.path.exists(target):
|
||||
os.remove(target)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"本地存储删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('本地存储删除失败')
|
||||
raise
|
||||
|
||||
def _sync_exists(self, remote_path: str) -> bool:
|
||||
return os.path.exists(self._abs_path(remote_path))
|
||||
@@ -65,8 +68,9 @@ class LocalStorageAdapter(BaseStorageAdapter):
|
||||
base_dir = self._abs_path(prefix) if prefix else self.root
|
||||
try:
|
||||
entries = os.scandir(base_dir)
|
||||
except OSError as e:
|
||||
raise CustomException(msg=f"本地存储列表失败: {e!s}", status_code=500)
|
||||
except OSError:
|
||||
logger.exception('本地存储列表失败')
|
||||
raise
|
||||
|
||||
result: list[StorageObject] = []
|
||||
for entry in entries:
|
||||
@@ -92,21 +96,24 @@ class LocalStorageAdapter(BaseStorageAdapter):
|
||||
def _sync_mkdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
os.makedirs(self._abs_path(remote_dir), exist_ok=True)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"本地存储创建目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('本地存储创建目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rmdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
os.rmdir(self._abs_path(remote_dir))
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"本地存储删除目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('本地存储删除目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rename(self, src: str, dst: str) -> None:
|
||||
try:
|
||||
os.makedirs(os.path.dirname(self._abs_path(dst)), exist_ok=True)
|
||||
shutil.move(self._abs_path(src), self._abs_path(dst))
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"本地存储重命名失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('本地存储重命名失败')
|
||||
raise
|
||||
|
||||
def _sync_copy(self, src: str, dst: str) -> None:
|
||||
src_abs, dst_abs = self._abs_path(src), self._abs_path(dst)
|
||||
@@ -116,5 +123,6 @@ class LocalStorageAdapter(BaseStorageAdapter):
|
||||
else:
|
||||
os.makedirs(os.path.dirname(dst_abs), exist_ok=True)
|
||||
shutil.copy2(src_abs, dst_abs)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"本地存储复制失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('本地存储复制失败')
|
||||
raise
|
||||
|
||||
@@ -176,7 +176,8 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
content=f,
|
||||
)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 上传失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 上传失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 上传失败: {self._error_desc(resp)}")
|
||||
return remote_path
|
||||
part_size, concurrency, _ = self._multipart_settings()
|
||||
resp = self.client.uploadFile(
|
||||
@@ -188,11 +189,13 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
enableCheckpoint=True,
|
||||
)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 上传失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 上传失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 上传失败: {self._error_desc(resp)}")
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 上传失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 上传失败')
|
||||
raise
|
||||
return remote_path
|
||||
|
||||
def _sync_download(self, remote_path: str, local_path: str) -> str:
|
||||
@@ -207,22 +210,26 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
enableCheckpoint=True,
|
||||
)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 下载失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 下载失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 下载失败: {self._error_desc(resp)}")
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 下载失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 下载失败')
|
||||
raise
|
||||
return local_path
|
||||
|
||||
def _sync_delete(self, remote_path: str) -> None:
|
||||
try:
|
||||
resp = self.client.deleteObject(bucketName=self._require_bucket(), objectKey=remote_path)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 删除失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 删除失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 删除失败: {self._error_desc(resp)}")
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 下载失败')
|
||||
raise
|
||||
|
||||
def _sync_exists(self, remote_path: str) -> bool:
|
||||
try:
|
||||
@@ -255,7 +262,8 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
kwargs["marker"] = marker
|
||||
resp = self.client.listObjects(**kwargs)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 列表失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 列表失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 列表失败: {self._error_desc(resp)}")
|
||||
body = getattr(resp, "body", None)
|
||||
for common in getattr(body, "commonPrefixs", None) or []:
|
||||
raw_key = (getattr(common, "prefix", "") or "").rstrip("/")
|
||||
@@ -291,8 +299,9 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
return result
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_page(self, prefix: str, page_size: int, cursor: str | None) -> StoragePage:
|
||||
"""游标分页:单次 SDK 请求只拉一页(OBS marker),翻页经前端回传游标。"""
|
||||
@@ -307,7 +316,8 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
kwargs["marker"] = cursor
|
||||
resp = self.client.listObjects(**kwargs)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 列表失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 列表失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 列表失败: {self._error_desc(resp)}")
|
||||
body = getattr(resp, "body", None)
|
||||
truncated = bool(getattr(body, "is_truncated", None))
|
||||
next_cursor = getattr(body, "next_marker", None) or None
|
||||
@@ -338,29 +348,33 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
)
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_buckets(self) -> list[str]:
|
||||
"""列出账号下全部存储桶。"""
|
||||
try:
|
||||
resp = self.client.listBuckets()
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 桶列表失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 桶列表失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 桶列表失败: {self._error_desc(resp)}")
|
||||
body = getattr(resp, "body", None)
|
||||
buckets = getattr(body, "buckets", None) or []
|
||||
return [getattr(b, "name", "") or "" for b in buckets if getattr(b, "name", "")]
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 桶列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 桶列表失败')
|
||||
raise
|
||||
|
||||
def _sync_get_url(self, remote_path: str, expire: int) -> str:
|
||||
try:
|
||||
resp = self.client.createSignedUrl("GET", bucketName=self._require_bucket(), objectKey=remote_path, expires=expire)
|
||||
return getattr(resp, "signedUrl", "")
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 生成预签名 URL 失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 桶列表失败')
|
||||
raise
|
||||
|
||||
# ── 目录操作(对象存储以 key/ 占位对象模拟目录)──────────────────
|
||||
|
||||
@@ -374,7 +388,8 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
kwargs["marker"] = marker
|
||||
resp = self.client.listObjects(**kwargs)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 列举对象失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 列举对象失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 列举对象失败: {self._error_desc(resp)}")
|
||||
body = getattr(resp, "body", None)
|
||||
for obj in getattr(body, "contents", None) or []:
|
||||
key = getattr(obj, "key", "")
|
||||
@@ -403,21 +418,25 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
try:
|
||||
resp = self.client.putContent(bucketName=self._require_bucket(), objectKey=remote_dir.rstrip("/") + "/", content=b"")
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 创建目录失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 创建目录失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 创建目录失败: {self._error_desc(resp)}")
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 创建目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 创建目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rmdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
resp = self.client.deleteObject(bucketName=self._require_bucket(), objectKey=remote_dir.rstrip("/") + "/")
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 删除目录失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 删除目录失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 删除目录失败: {self._error_desc(resp)}")
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 删除目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 创建目录失败')
|
||||
raise
|
||||
|
||||
def _sync_copy(self, src: str, dst: str) -> None:
|
||||
"""复制:目录走基类递归实现;文件用服务端 copyObject。"""
|
||||
@@ -432,11 +451,13 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
destObjectKey=dst,
|
||||
)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 复制失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 复制失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 复制失败: {self._error_desc(resp)}")
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 复制失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 复制失败')
|
||||
raise
|
||||
|
||||
def _sync_rename(self, src: str, dst: str) -> None:
|
||||
"""重命名/移动:优先 OBS 原生 renameFile(支持目录级),失败回退先复制后删除。"""
|
||||
@@ -455,8 +476,9 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
def _sync_list_recursive(self, prefix: str) -> list[StorageObject]:
|
||||
try:
|
||||
keys = self._list_all_keys(prefix)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 递归列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 递归列表失败')
|
||||
raise
|
||||
return self._entries_from_keys(keys)
|
||||
|
||||
def _sync_delete_dir(self, remote_dir: str) -> None:
|
||||
@@ -473,11 +495,13 @@ class ObsStorageAdapter(BaseStorageAdapter):
|
||||
deleteObjectsRequest={"quiet": True, "objects": [{"key": k} for k in batch]},
|
||||
)
|
||||
if not self._is_ok(resp):
|
||||
raise CustomException(msg=f"OBS 递归删除失败: {self._error_desc(resp)}", status_code=500)
|
||||
logger.error(f"OBS 递归删除失败: {self._error_desc(resp)}")
|
||||
raise RuntimeError(f"OBS 递归删除失败: {self._error_desc(resp)}")
|
||||
except CustomException:
|
||||
raise
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OBS 递归删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OBS 递归删除失败')
|
||||
raise
|
||||
|
||||
def _sync_close(self) -> None:
|
||||
"""关闭 OBS 客户端连接。"""
|
||||
|
||||
@@ -49,7 +49,7 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
cfg.disable_download_crc64_check = adv.disable_download_crc64_check
|
||||
cfg.enabled_redirect = adv.enabled_redirect
|
||||
cfg.use_dualstack_endpoint = adv.use_dualstack_endpoint
|
||||
self.client = oss.Client(cfg)
|
||||
self.client: oss.Client = oss.Client(cfg)
|
||||
|
||||
def _sync_test_connection(self) -> bool:
|
||||
try:
|
||||
@@ -73,8 +73,9 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
part_size=part_size,
|
||||
parallel_num=concurrency,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 上传失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 上传失败')
|
||||
raise
|
||||
return remote_path
|
||||
|
||||
def _sync_download(self, remote_path: str, local_path: str) -> str:
|
||||
@@ -83,15 +84,17 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
oss.GetObjectRequest(bucket=self.bucket_name, key=remote_path),
|
||||
local_path,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 下载失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 下载失败')
|
||||
raise
|
||||
return local_path
|
||||
|
||||
def _sync_delete(self, remote_path: str) -> None:
|
||||
try:
|
||||
self.client.delete_object(oss.DeleteObjectRequest(bucket=self.bucket_name, key=remote_path))
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 删除失败')
|
||||
raise
|
||||
|
||||
def _sync_exists(self, remote_path: str) -> bool:
|
||||
try:
|
||||
@@ -139,8 +142,9 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
)
|
||||
)
|
||||
return result
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_page(self, prefix: str, page_size: int, cursor: str | None) -> StoragePage:
|
||||
"""游标分页:单次 SDK 请求只拉一页(OSS continuation_token),翻页经前端回传游标。"""
|
||||
@@ -177,8 +181,9 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
)
|
||||
)
|
||||
return StoragePage(items=items, has_next=truncated, next_cursor=next_cursor)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_buckets(self) -> list[str]:
|
||||
"""列出账号下全部存储桶。"""
|
||||
@@ -190,8 +195,9 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
if b.name:
|
||||
result.append(b.name)
|
||||
return result
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 桶列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 桶列表失败')
|
||||
raise
|
||||
|
||||
def _sync_get_url(self, remote_path: str, expire: int) -> str | None:
|
||||
try:
|
||||
@@ -200,8 +206,9 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
expires=timedelta(seconds=expire),
|
||||
)
|
||||
return result.url
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 生成预签名 URL 失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 生成预签名 URL 失败')
|
||||
raise
|
||||
|
||||
# ── 目录操作(对象存储以 key/ 占位对象模拟目录)──────────────────
|
||||
|
||||
@@ -230,14 +237,16 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
def _sync_mkdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self.client.put_object(oss.PutObjectRequest(bucket=self.bucket_name, key=remote_dir.rstrip("/") + "/", body=b""))
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 创建目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 创建目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rmdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self.client.delete_object(oss.DeleteObjectRequest(bucket=self.bucket_name, key=remote_dir.rstrip("/") + "/"))
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 删除目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 删除目录失败')
|
||||
raise
|
||||
|
||||
def _sync_copy(self, src: str, dst: str) -> None:
|
||||
"""复制:目录走基类递归实现;文件用服务端 copy_object。"""
|
||||
@@ -248,8 +257,9 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
self.client.copy_object(
|
||||
oss.CopyObjectRequest(bucket=self.bucket_name, key=dst, source_bucket=self.bucket_name, source_key=src)
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 复制失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 复制失败')
|
||||
raise
|
||||
|
||||
def _sync_rename(self, src: str, dst: str) -> None:
|
||||
"""重命名/移动:目录先复制后删除;文件 copy_object 后删源。"""
|
||||
@@ -262,8 +272,9 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
def _sync_list_recursive(self, prefix: str) -> list[StorageObject]:
|
||||
try:
|
||||
keys = self._list_all_keys(prefix)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 递归列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 递归列表失败')
|
||||
raise
|
||||
return self._entries_from_keys(keys)
|
||||
|
||||
def _sync_delete_dir(self, remote_dir: str) -> None:
|
||||
@@ -281,5 +292,6 @@ class OssStorageAdapter(BaseStorageAdapter):
|
||||
quiet=True,
|
||||
)
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"OSS 递归删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('OSS 递归删除失败')
|
||||
raise
|
||||
|
||||
@@ -87,22 +87,25 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
max_concurrency=concurrency,
|
||||
)
|
||||
self.client.upload_file(local_path, self._require_bucket(), remote_path, Config=transfer_config)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 上传失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 上传失败')
|
||||
raise
|
||||
return remote_path
|
||||
|
||||
def _sync_download(self, remote_path: str, local_path: str) -> str:
|
||||
try:
|
||||
self.client.download_file(self._require_bucket(), remote_path, local_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 下载失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 下载失败')
|
||||
raise
|
||||
return local_path
|
||||
|
||||
def _sync_delete(self, remote_path: str) -> None:
|
||||
try:
|
||||
self.client.delete_object(Bucket=self._require_bucket(), Key=remote_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 删除失败')
|
||||
raise
|
||||
|
||||
def _sync_exists(self, remote_path: str) -> bool:
|
||||
try:
|
||||
@@ -162,8 +165,9 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
else:
|
||||
break
|
||||
return result
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_page(self, prefix: str, page_size: int, cursor: str | None) -> StoragePage:
|
||||
"""游标分页:单次 SDK 请求只拉一页(S3 ContinuationToken),翻页经前端回传游标。"""
|
||||
@@ -202,16 +206,18 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
has_next=bool(resp.get("IsTruncated")),
|
||||
next_cursor=resp.get("NextContinuationToken"),
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 列表失败')
|
||||
raise
|
||||
|
||||
def _sync_list_buckets(self) -> list[str]:
|
||||
"""列出账号下全部存储桶。"""
|
||||
try:
|
||||
resp = self.client.list_buckets()
|
||||
return [b.get("Name", "") for b in resp.get("Buckets", []) if b.get("Name")]
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 桶列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 桶列表失败')
|
||||
raise
|
||||
|
||||
def _sync_get_url(self, remote_path: str, expire: int) -> str:
|
||||
try:
|
||||
@@ -220,8 +226,9 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
Params={"Bucket": self._require_bucket(), "Key": remote_path},
|
||||
ExpiresIn=expire,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 生成预签名 URL 失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 生成预签名 URL 失败')
|
||||
raise
|
||||
|
||||
def _sync_close(self) -> None:
|
||||
"""关闭 boto3 客户端连接。"""
|
||||
@@ -252,14 +259,16 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
def _sync_mkdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self.client.put_object(Bucket=self._require_bucket(), Key=remote_dir.rstrip("/") + "/", Body=b"")
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 创建目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 创建目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rmdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self.client.delete_object(Bucket=self._require_bucket(), Key=remote_dir.rstrip("/") + "/")
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 删除目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 删除目录失败')
|
||||
raise
|
||||
|
||||
def _sync_copy(self, src: str, dst: str) -> None:
|
||||
"""复制:目录走基类递归实现;文件用服务端 copy_object。"""
|
||||
@@ -272,8 +281,9 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
CopySource={"Bucket": self._require_bucket(), "Key": src},
|
||||
Key=dst,
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 复制失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 复制失败')
|
||||
raise
|
||||
|
||||
def _sync_rename(self, src: str, dst: str) -> None:
|
||||
"""重命名/移动:目录先复制后删除;文件 copy_object 后删源。"""
|
||||
@@ -286,8 +296,9 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
def _sync_list_recursive(self, prefix: str) -> list[StorageObject]:
|
||||
try:
|
||||
keys = self._list_all_keys(prefix)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 递归列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 递归列表失败')
|
||||
raise
|
||||
return self._entries_from_keys(keys)
|
||||
|
||||
def _sync_delete_dir(self, remote_dir: str) -> None:
|
||||
@@ -302,5 +313,6 @@ class S3StorageAdapter(BaseStorageAdapter):
|
||||
Bucket=self._require_bucket(),
|
||||
Delete={"Objects": [{"Key": k} for k in keys[i : i + 1000]], "Quiet": True},
|
||||
)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"S3 递归删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('S3 递归删除失败')
|
||||
raise
|
||||
|
||||
@@ -117,8 +117,9 @@ class SftpStorageAdapter(BaseStorageAdapter):
|
||||
if dir_part:
|
||||
self._ensure_remote_dir(client, dir_part)
|
||||
client.put(local_path, remote_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 上传失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 上传失败')
|
||||
raise
|
||||
return remote_path
|
||||
|
||||
def _sync_download(self, remote_path: str, local_path: str) -> str:
|
||||
@@ -140,15 +141,17 @@ class SftpStorageAdapter(BaseStorageAdapter):
|
||||
lf.write(chunk)
|
||||
else:
|
||||
client.get(remote_path, local_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 下载失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 下载失败')
|
||||
raise
|
||||
return local_path
|
||||
|
||||
def _sync_delete(self, remote_path: str) -> None:
|
||||
try:
|
||||
self._connect().remove(remote_path)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 删除失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 删除失败')
|
||||
raise
|
||||
|
||||
def _sync_exists(self, remote_path: str) -> bool:
|
||||
try:
|
||||
@@ -161,8 +164,9 @@ class SftpStorageAdapter(BaseStorageAdapter):
|
||||
try:
|
||||
# 空 prefix 表示浏览根目录:SFTP 用 "." 表示登录后的当前目录
|
||||
attrs = self._connect().listdir_attr(prefix or ".")
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 列表失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 列表失败')
|
||||
raise
|
||||
result: list[StorageObject] = []
|
||||
for attr in attrs:
|
||||
result.append(
|
||||
@@ -188,20 +192,23 @@ class SftpStorageAdapter(BaseStorageAdapter):
|
||||
def _sync_mkdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self._ensure_remote_dir(self._connect(), remote_dir)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 创建目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 创建目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rmdir(self, remote_dir: str) -> None:
|
||||
try:
|
||||
self._connect().rmdir(remote_dir)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 删除目录失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 删除目录失败')
|
||||
raise
|
||||
|
||||
def _sync_rename(self, src: str, dst: str) -> None:
|
||||
try:
|
||||
self._connect().posix_rename(src, dst)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 重命名失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 重命名失败')
|
||||
raise
|
||||
|
||||
def _sync_copy(self, src: str, dst: str) -> None:
|
||||
"""复制:目录走基类递归实现;文件用通道流式复制(SFTP 无服务端复制)。"""
|
||||
@@ -213,5 +220,6 @@ class SftpStorageAdapter(BaseStorageAdapter):
|
||||
with client.open(src, "rb") as rf, client.open(dst, "wb") as wf:
|
||||
while chunk := rf.read(1024 * 1024):
|
||||
wf.write(chunk)
|
||||
except Exception as e:
|
||||
raise CustomException(msg=f"SFTP 复制失败: {e!s}", status_code=500)
|
||||
except Exception:
|
||||
logger.exception('SFTP 复制失败')
|
||||
raise
|
||||
|
||||
Reference in New Issue
Block a user