import OSS from "ali-oss"; import type { AppConfig } from "@tablestore/tablestore-agent-shared"; /** * Build an `ali-oss` client from a resolved AppConfig, or `null` if no OSS * target has been configured for this instance. Callers should treat `null` * as the `OSS_NOT_CONFIGURED` case — the upstream error envelope. * * Construction mirrors the upstream agent-storage SDK's OSSUploader (which is * the path that successfully uploads files for the local-upload tab): * v1 signing (no `authorizationV4`), raw region, explicit endpoint. We do * NOT mirror `packages/sdk/src/managed-oss.ts` here — that file is the * bucket-creation path, which talks to a different OSS surface that requires * v4. Forcing v4 on object-operation calls produces "SignatureDoesNotMatch" * because the canonical string is computed against a region that the * legacy bucket signing flow doesn't agree with. */ export function getOssClient(config: AppConfig): OSS | null { const endpoint = (config.oss_endpoint ?? "").trim(); const bucket = (config.oss_bucket_name ?? "").trim(); const region = (config.region ?? "").trim(); if (!endpoint || !bucket || !region) return null; return new OSS({ accessKeyId: config.access_key_id ?? "", accessKeySecret: config.access_key_secret ?? "", bucket, region, endpoint, secure: true, }); } export interface OssErrorDetails { upstreamCode?: string; upstreamRequestId?: string; upstreamHostId?: string; upstreamStatus?: number; [key: string]: unknown; } export interface MappedOssError { code: string; httpStatus: number; message: string; details: OssErrorDetails; } /** * Translate an error from ali-oss (or anywhere else in the OSS path) into * the dashboard's canonical `{ code, httpStatus, message, details }` shape. * Upstream identifiers (`code`, `requestId`, `hostId`, HTTP status) are * preserved on `details` so the UI can prefix `[SignatureDoesNotMatch]` and * support agents can match the request against OSS-side logs. */ export function mapOssError(error: unknown): MappedOssError { const record = error && typeof error === "object" ? (error as Record) : {}; const ossCode = typeof record.code === "string" ? record.code : ""; const requestId = typeof record.requestId === "string" ? record.requestId : undefined; const hostId = typeof record.hostId === "string" ? record.hostId : undefined; const status = typeof record.status === "number" ? record.status : typeof record.statusCode === "number" ? record.statusCode : undefined; const message = error instanceof Error ? error.message : String(error ?? "Unknown OSS error"); const details: OssErrorDetails = { upstreamCode: ossCode || undefined, upstreamRequestId: requestId, upstreamHostId: hostId, upstreamStatus: status, }; switch (ossCode) { case "AccessDenied": case "InvalidAccessKeyId": case "SignatureDoesNotMatch": return { code: "OSS_FORBIDDEN", httpStatus: 403, message, details }; case "NoSuchBucket": return { code: "OSS_BUCKET_NOT_FOUND", httpStatus: 404, message, details, }; case "InvalidArgument": return { code: "BAD_REQUEST", httpStatus: 400, message, details }; default: return { code: "OSS_UPSTREAM_ERROR", httpStatus: 502, message, details }; } }