yikart/AiToEarn · error · Error

文件上传失败

Error message

文件上传失败

What it means

upFileStream in oss.service uploads a file stream via Aliyun OSS and throws '文件上传失败' when the OSS client response status is not 200. The upload reached OSS but res.status signaled failure, so the file was not successfully stored.

Source

Thrown at project/aitoearn-electron/server/src/lib/oss/oss.service.ts:88

    const tempStr = mimetype.split('/');
    const fileTypeStr = tempStr[tempStr.length - 1];

    const stream = new Duplex();
    stream.push(buffer);
    stream.push(null);

    try {
      const upRes = await this.client.putStream(
        `${path}/${newName}.${fileTypeStr}`,
        stream,
      );

      const {
        name,
        res: { status },
      } = upRes;

      if (status !== HttpStatus.OK) throw new Error('文件上传失败');

      return {
        name,
      };
    } catch (error) {
      throw error;
    }
  }

  /**
   * 去除文件前置
   * @param filePath
   */
  private noHostFilePath(filePath: string) {
    const hostUrl = this.configService.get('OSS_CONFIG.HOST_URL');

    const _hostUrl = hostUrl.replace('https', 'http');
    if (filePath.indexOf(_hostUrl) === 0) {

View on GitHub (pinned to d3aa8bea5b)

Solutions

  1. Log the actual upRes.res.status and request-id to identify the OSS error.
  2. Refresh OSS credentials (AK/SK or STS token) and verify env config.
  3. Verify bucket name, endpoint region, and write permissions.
  4. Retry transient failures (5xx/timeouts) with backoff; check network to OSS.

Example fix

// before
if (status !== HttpStatus.OK) throw new Error('文件上传失败');
// after
if (status !== HttpStatus.OK) {
  console.error('OSS upload failed', { status, requestId: upRes.res.requestId, name });
  throw new Error(`文件上传失败: OSS status ${status} (requestId=${upRes.res.requestId})`);
}
Defensive patterns

Strategy: retry

Validate before calling

function assertOssConfig(cfg) {
  const required = ['region', 'accessKeyId', 'accessKeySecret', 'bucket'];
  for (const k of required) {
    if (!cfg[k]) throw new Error(`OSS 配置缺失: ${k}`);
  }
}
assertOssConfig(ossConfig);

Try / catch

let backoff = 1;
try {
  await ossService.upFileStream(stream, key);
} catch (e) {
  if (e.message === '文件上传失败') {
    await delay(backoff++ * 1000);
    return retryUpload(stream, key); // 5xx/超时可重试
  }
  throw e;
}

Prevention

When it happens

Trigger: OSS putStream returns upRes.res.status !== HttpStatus.OK — invalid/expired STS credentials, bucket permission denied, network partial failure, wrong bucket/endpoint, or OSS returning 4xx/5xx.

Common situations: Expired AccessKeyId/Secret/STS token; bucket ACL changes; wrong region endpoint configured; oversized streams hitting timeouts; OSS throttling (503).

Related errors


AI-assisted analysis of yikart/AiToEarn@d3aa8bea5b (2026-08-31). Data as JSON: /api/errors/df75fa79ac45ccbc. Report an issue: GitHub.