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:
zhangtao
2026-10-04 05:44:21 +08:00
parent c73daa7eae
commit 644d262cad
31 changed files with 824 additions and 363 deletions
+10 -7
View File
@@ -10,6 +10,7 @@ from agno.db.postgres import AsyncPostgresDb
from agno.db.sqlite import AsyncSqliteDb
from agno.session.team import TeamSession
from app.common.enums import RET
from app.config.setting import settings
from app.core.base_schema import AuthSchema
from app.core.exceptions import CustomException
@@ -43,7 +44,8 @@ def _get_agno_db() -> Any:
_db_instance = AsyncSqliteDb(db_file=db_uri.replace("sqlite+aiosqlite:///", ""))
else:
# settings.DATABASE_TYPE 是服务端配置:取值不在支持范围属部署配置错误,按 500 返回
raise CustomException(msg=f"不支持的数据库类型: {db_type}", status_code=500)
logger.error(f"不支持的数据库类型: {db_type}")
raise RuntimeError(f"不支持的数据库类型: {db_type}")
return _db_instance
@@ -142,7 +144,7 @@ class ChatSessionCRUD:
)
except Exception as e:
logger.error(f"获取会话列表失败: {e}")
raise CustomException(msg="获取会话列表失败", status_code=500) from e
raise
rows: list[dict[str, Any]] = []
total = 0
@@ -174,9 +176,10 @@ class ChatSessionCRUD:
result = await self.db.upsert_session(session=session)
except Exception as e:
logger.exception(f"创建会话失败: {e}")
raise CustomException(msg="创建会话失败", status_code=500) from e
raise
if result is None:
raise CustomException(msg="创建会话失败", status_code=500)
logger.error("创建会话失败")
raise RuntimeError("创建会话失败")
return result
async def rename(self, session_id: str, data: ChatSessionUpdateSchema) -> None:
@@ -190,9 +193,9 @@ class ChatSessionCRUD:
)
except Exception as e:
logger.error(f"更新会话失败: {e}")
raise CustomException(msg="更新会话失败", status_code=500) from e
raise
if result is None:
raise CustomException(msg="会话不存在", code=10404, status_code=404)
raise CustomException(msg="会话不存在", code=RET.NOT_FOUND.code)
async def delete(self, session_ids: list[str]) -> None:
"""批量删除会话"""
@@ -202,4 +205,4 @@ class ChatSessionCRUD:
except Exception as e:
logger.error(f"删除会话失败: {session_id} - {e}")
# agno/SQLite 侧删除失败属内部故障,按 500 返回(session_ids 为客户端参数,但失败原因在服务端)
raise CustomException(msg=f"删除会话失败: {session_id}", status_code=500) from e
raise
+7 -7
View File
@@ -10,7 +10,7 @@ from agno.run.team import TeamRunOutput
from agno.session.team import TeamSession
from redis.asyncio import Redis
from app.common.enums import RedisInitKeyConfig
from app.common.enums import RET, RedisInitKeyConfig
from app.core.base_schema import AuthSchema, PageResultSchema
from app.core.exceptions import CustomException
from app.core.logger import logger
@@ -277,7 +277,7 @@ class ChatService:
crud = ChatSessionCRUD(self.auth)
session = await crud.get_by_id(session_id=session_id)
if not session:
raise CustomException(msg="会话不存在", code=10404, status_code=404)
raise CustomException(msg="会话不存在", code=RET.NOT_FOUND.code)
return ChatSessionOutSchema.model_validate(_format_session_data(session))
async def create(self, data: ChatSessionCreateSchema) -> ChatSessionOutSchema:
@@ -311,7 +311,7 @@ class ChatService:
async def update(self, session_id: str, data: ChatSessionUpdateSchema) -> None:
crud = ChatSessionCRUD(self.auth)
if not await crud.get_by_id(session_id=session_id):
raise CustomException(msg="会话不存在", code=10404, status_code=404)
raise CustomException(msg="会话不存在", code=RET.NOT_FOUND.code)
await crud.rename(session_id=session_id, data=data)
async def delete(self, session_ids: list[str]) -> None:
@@ -320,7 +320,7 @@ class ChatService:
crud = ChatSessionCRUD(self.auth)
for session_id in session_ids:
if not await crud.get_by_id(session_id=session_id):
raise CustomException(msg=f"会话不存在: {session_id}", code=10404, status_code=404)
raise CustomException(msg=f"会话不存在: {session_id}", code=RET.NOT_FOUND.code)
await crud.delete(session_ids=session_ids)
@@ -518,15 +518,15 @@ class AiModelConfigService:
async def update(self, config_id: str, config: AiModelConfigSchema) -> dict[str, Any]:
result = await update_user_model_config(self.redis, self._user_id, config_id, config)
if result is None:
raise CustomException(msg="模型配置不存在", code=10404, status_code=404)
raise CustomException(msg="模型配置不存在", code=RET.NOT_FOUND.code)
return result
async def delete(self, config_id: str) -> None:
ok = await delete_user_model_config(self.redis, self._user_id, config_id)
if not ok:
raise CustomException(msg="模型配置不存在", code=10404, status_code=404)
raise CustomException(msg="模型配置不存在", code=RET.NOT_FOUND.code)
async def set_active(self, config_id: str) -> None:
ok = await set_active_model_config(self.redis, self._user_id, config_id)
if not ok:
raise CustomException(msg="模型配置不存在", code=10404, status_code=404)
raise CustomException(msg="模型配置不存在", code=RET.NOT_FOUND.code)
@@ -61,10 +61,11 @@ def handle_service_exception(func: Callable) -> Callable:
return await func(*args, **kwargs)
except CustomException:
raise
except Exception as e:
except Exception:
# 服务层公共出口:这里捕获到的都是**非业务**异常(模板渲染/DB/反射等内部故障),
# 按 500 返回,避免监控把服务端故障误判为客户端错误;业务异常由上面的分支原样透传(保留自身 4xx)
raise CustomException(msg=f"{func.__name__}执行失败: {e!s}", status_code=500)
# 记日志后原样抛出让全局异常处理器统一映射为 5xx;业务异常由上面的分支原样透传(保留自身 4xx)
logger.exception(f"{func.__name__}执行失败")
raise
return wrapper
@@ -372,8 +373,9 @@ class GenTableService:
return True
except CustomException:
raise
except Exception as e:
raise CustomException(msg=f"导入失败, {e!s}", status_code=500)
except Exception:
logger.exception('导入业务表信息失败')
raise
@handle_service_exception
async def create_table(self, sql: str) -> bool | None:
@@ -456,8 +458,9 @@ class GenTableService:
except CustomException:
raise
except Exception as e:
raise CustomException(msg=f"创建表结构失败: {e!s}", status_code=500)
except Exception:
logger.exception('创建表结构失败')
raise
@handle_service_exception
async def update_gen_table(self, data: GenTableSchema, table_id: int) -> GenTableOutSchema:
@@ -518,8 +521,9 @@ class GenTableService:
return out
except CustomException:
raise
except Exception as e:
raise CustomException(msg=str(e), status_code=500)
except Exception:
logger.exception('更新业务表信息失败')
raise
else:
raise CustomException(msg="业务表不存在")
@@ -543,8 +547,9 @@ class GenTableService:
await GenTableColumnCRUD(self.auth, self.db).delete_gen_table_column_by_table_id_crud(ids)
# 再删除表信息
await GenTableCRUD(self.auth, self.db).delete_gen_table(ids)
except Exception as e:
raise CustomException(msg=str(e), status_code=500)
except Exception:
logger.exception('删除业务表信息失败')
raise
@handle_service_exception
async def get_gen_table_by_id(self, table_id: int) -> GenTableOutSchema:
@@ -711,8 +716,9 @@ class GenTableService:
if not init_path.exists():
os.makedirs(str(d), exist_ok=True)
await anyio.Path(str(init_path)).write_text("# -*- coding: utf-8 -*-", encoding="utf-8")
except Exception as e:
raise CustomException(msg=f"渲染模板失败,表名:{table_schema.table_name},详细错误信息:{e!s}", status_code=500)
except Exception:
logger.exception(f"渲染模板失败,表名:{table_schema.table_name}")
raise
safe_schema: GenTableOutSchema = render_info[4]
reference_ctx = Jinja2TemplateUtil.prepare_context(Jinja2TemplateUtil.sentinel_render_schema(safe_schema))
@@ -1014,8 +1020,9 @@ class GenTableService:
sub_cfg = await GenTableCRUD(self.auth, self.db).get_gen_table_by_name(sn)
if sub_cfg:
await self.sync_db(sn, _sync_sub=False)
except Exception as e:
raise CustomException(msg=f"同步失败: {e!s}", status_code=500)
except Exception:
logger.exception('同步失败')
raise
async def hydrate_sub_table(self, gen_table: GenTableOutSchema) -> None:
"""主子表:优先使用已导入的子表配置,否则回退为只读 DB 结构。
@@ -104,7 +104,7 @@ async def oauth_login_redirect_controller(
provider=provider,
redirect_uri=redirect_uri,
)
return RedirectContentResponse(url=url, status_code=302)
return RedirectContentResponse(url, 302)
@AuthRouter.get("/oauth/{provider}/callback", summary="第三方OAuth回调", include_in_schema=False)
@@ -125,7 +125,7 @@ async def oauth_callback_controller(
code=code,
state=state,
)
return RedirectContentResponse(url=url, status_code=302)
return RedirectContentResponse(url, 302)
# =================================================== #
@@ -73,7 +73,8 @@ def _require_credentials(provider: OAuthProvider) -> tuple[str, str]:
raise CustomException(msg="不支持的 OAuth 渠道")
if not cid or not sec:
# 渠道密钥为空属**部署配置缺失**:客户端无论怎么改请求都无法成功,按 500 返回以免监控误判为客户端错误
raise CustomException(msg=f"{provider} OAuth 未配置(客户端密钥为空)", status_code=500)
logger.error(f"{provider} OAuth 未配置(客户端密钥为空)")
raise RuntimeError(f"{provider} OAuth 未配置(客户端密钥为空)")
return cid, sec
@@ -137,7 +138,7 @@ async def _http_json(method: str, url: str, **kwargs: Any) -> Any:
except json.JSONDecodeError:
text = r.text
logger.error(f"OAuth 非 JSON 响应: {text[:500]}")
raise CustomException(msg="OAuth 接口返回异常", status_code=500)
raise
async def _http_text(method: str, url: str, **kwargs: Any) -> str:
@@ -438,6 +439,10 @@ async def start_oauth_login(
)
except CustomException as e:
return _frontend_error_redirect(redirect_uri or fallback, e.msg)
except Exception:
# 浏览器跳转边界:意外故障不向用户抛裸 500,细节进日志、用户回到错误页(t19)
logger.exception("OAuth 授权入口处理失败")
return _frontend_error_redirect(redirect_uri or fallback, "登录服务暂时不可用,请稍后重试")
async def finish_oauth_login(
@@ -472,6 +477,10 @@ async def finish_oauth_login(
)
except CustomException as e:
return _frontend_error_redirect(await _frontend(), e.msg)
except Exception:
# 同上:OAuth 回调是浏览器跳转边界,意外故障统一回错误页
logger.exception("OAuth 回调处理失败")
return _frontend_error_redirect(await _frontend(), "登录服务暂时不可用,请稍后重试")
return _frontend_success_redirect(
frontend,
token.access_token,
+2 -1
View File
@@ -83,7 +83,8 @@ async def auto_register_login_user(
return existing
# 原始异常被吞掉(可能是 DB/唯一约束/驱动等未知服务端故障):按 500 返回,
# 避免监控把内部故障误判为客户端错误;确实的客户端问题由 UserService 的 CustomException 透传
raise CustomException(msg=fail_msg, status_code=500)
logger.exception('自动注册第三方登录用户失败')
raise
user = await UserCRUD(auth, db).get(username=username)
if not user:
raise CustomException(msg=fail_msg)
@@ -67,7 +67,7 @@ async def _http_json(method: str, url: str, **kwargs: Any) -> dict:
return r.json()
except json.JSONDecodeError:
logger.error(f"微信 API 非 JSON 响应: {r.text[:500]}")
raise CustomException(msg="微信接口返回异常", status_code=500)
raise
async def _get_access_token(redis: Redis) -> str:
+9 -9
View File
@@ -121,7 +121,7 @@ class DictTypeService:
logger.info(f"创建字典类型成功: {new_obj_dict}")
except Exception as e:
logger.error(f"创建字典类型失败: {e}")
raise CustomException(msg="同步字典类型缓存失败", status_code=500) from e
raise
return new_obj_dict
@@ -176,7 +176,7 @@ class DictTypeService:
logger.info(f"更新字典类型成功并刷新缓存: {new_obj_dict}")
except Exception as e:
logger.error(f"更新字典类型缓存失败: {e}")
raise CustomException(msg="同步字典类型缓存失败", status_code=500) from e
raise
return new_obj_dict
@@ -215,7 +215,7 @@ class DictTypeService:
logger.info(f"删除字典类型缓存: {dt}")
except Exception as e:
logger.error(f"删除字典类型缓存失败: {e}")
raise CustomException(msg="同步删除字典缓存失败", status_code=500) from e
raise
await DictTypeCRUD(self.auth, self.db).delete(ids=ids)
async def set_available(self, data: BatchSetAvailable) -> None:
@@ -330,7 +330,7 @@ class DictDataService:
except Exception as e:
logger.error(f"❌️ 字典初始化过程发生错误: {e}")
raise CustomException(msg="字典数据初始化失败", status_code=500) from e
raise
@staticmethod
async def get_init_cache(redis: Redis, dict_type: str) -> list[dict]:
@@ -376,7 +376,7 @@ class DictDataService:
raise
except Exception as e:
logger.error(f"获取字典缓存失败: {e!s}")
raise CustomException(msg="获取字典数据失败", status_code=500) from e
raise
async def _refresh_dict_cache(self, redis: Redis, dict_type: str) -> None:
"""刷新指定字典类型的 Redis 缓存
@@ -420,7 +420,7 @@ class DictDataService:
logger.info(f"创建字典数据写入缓存成功: {obj}")
except Exception as e:
logger.error(f"创建字典数据写入缓存失败: {e}")
raise CustomException(msg="同步字典数据缓存失败", status_code=500) from e
raise
return DictDataOutSchema.model_validate(obj)
@@ -462,7 +462,7 @@ class DictDataService:
await self._refresh_dict_cache(redis, dict_type.dict_type)
except Exception as e:
logger.error(f"刷新旧字典缓存失败: {e}")
raise CustomException(msg="同步旧字典数据缓存失败", status_code=500) from e
raise
obj = await DictDataCRUD(self.auth, self.db).update(id=id, data=data)
@@ -472,7 +472,7 @@ class DictDataService:
logger.info(f"更新字典数据写入缓存成功: {obj}")
except Exception as e:
logger.error(f"更新字典数据写入缓存失败: {e}")
raise CustomException(msg="同步字典数据缓存失败", status_code=500) from e
raise
return DictDataOutSchema.model_validate(obj)
@@ -504,7 +504,7 @@ class DictDataService:
logger.info(f"删除字典数据并刷新缓存: dict_type={dt}")
except Exception as e:
logger.error(f"删除字典数据刷新缓存失败: {e}")
raise CustomException(msg="同步删除字典数据缓存失败", status_code=500) from e
raise
await DictDataCRUD(self.auth, self.db).delete(ids=ids)
async def set_available(self, data: BatchSetAvailable) -> None:
+4 -3
View File
@@ -57,10 +57,11 @@ class ParamsService:
)
if not result:
logger.error(f"同步配置到缓存失败: {out}")
raise CustomException(msg="同步配置到缓存失败", status_code=500)
logger.error("同步配置到缓存失败")
raise RuntimeError("同步配置到缓存失败")
except Exception as e:
logger.error(f"更新系统配置失败: {e}")
raise CustomException(msg="同步配置到缓存失败", status_code=500) from e
raise
return out
@@ -95,7 +96,7 @@ class ParamsService:
await ParamsService._sync_configs_to_redis(redis, config_obj)
except Exception as e:
logger.error(f"❌️ 初始化系统参数到 Redis 失败: {e}")
raise CustomException(msg="初始化系统参数到 Redis 失败", status_code=500) from e
raise
@staticmethod
async def get_init_cache(redis: Redis) -> list[dict]:
+1 -1
View File
@@ -384,7 +384,7 @@ class UserService:
raise
except Exception as e:
logger.error(f"批量导入用户失败: {e!s}")
raise CustomException(msg=f"导入失败: {e!s}", status_code=500) from e
raise
async def _process_import_row(
self,
+10 -7
View File
@@ -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