(fileBuffer, originalFilename, authToken, account)
| 244 | const uploadToOssWithSts = async (fileBuffer, stsCredentials, ossInfo, fileContentTypeFull, retryCount = 0, account) => { |
| 245 | try { |
| 246 | // 参数验证 |
| 247 | if (!fileBuffer || !stsCredentials || !ossInfo) { |
| 248 | logger.error('缺少必要的上传参数', 'UPLOAD') |
| 249 | throw new Error('缺少必要的上传参数') |
| 250 | } |
| 251 | |
| 252 | const proxyAgent = getProxyAgent(account); |
| 253 | const client = new OSS({ |
| 254 | accessKeyId: stsCredentials.access_key_id, |
| 255 | accessKeySecret: stsCredentials.access_key_secret, |
| 256 | stsToken: stsCredentials.security_token, |
| 257 | bucket: ossInfo.bucket, |
| 258 | endpoint: ossInfo.endpoint, |
| 259 | secure: true, |
| 260 | agent: proxyAgent, |
| 261 | httpsAgent: proxyAgent, |
| 262 | urllib: createOssProxyClient(account), |
| 263 | timeout: UPLOAD_CONFIG.timeout |
| 264 | }) |
| 265 | |
| 266 | logger.info(`上传文件到OSS: ${ossInfo.path} (${fileBuffer.length} bytes)`, 'UPLOAD', '📤') |
| 267 | |
| 268 | const result = await client.put(ossInfo.path, fileBuffer, { |
| 269 | headers: { |
| 270 | 'Content-Type': fileContentTypeFull || 'application/octet-stream' |
| 271 | } |
| 272 | }) |
| 273 | |
| 274 | if (result.res && result.res.status === 200) { |
| 275 | logger.success('文件上传到OSS成功', 'UPLOAD') |
| 276 | return { success: true, result } |
| 277 | } else { |
| 278 | logger.error(`OSS上传失败,状态码: ${result.res?.status || 'unknown'}`, 'UPLOAD') |
| 279 | throw new Error(`OSS上传失败,状态码: ${result.res?.status || 'unknown'}`) |
| 280 | } |
| 281 | } catch (error) { |
| 282 | logger.error(`OSS上传失败 (重试: ${retryCount})`, 'UPLOAD', '', error) |
| 283 | |
| 284 | // 重试逻辑 |
| 285 | if (retryCount < UPLOAD_CONFIG.maxRetries) { |
| 286 | const delayMs = UPLOAD_CONFIG.retryDelay * Math.pow(2, retryCount) |
| 287 | logger.warn(`等待 ${delayMs}ms 后重试OSS上传...`, 'UPLOAD', '⏳') |
| 288 | await delay(delayMs) |
| 289 | |
| 290 | return uploadToOssWithSts(fileBuffer, stsCredentials, ossInfo, fileContentTypeFull, retryCount + 1, account); |
| 291 | } |
| 292 | |
| 293 | throw error |
| 294 | } |
no test coverage detected