You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Autodesk OSS v2 API上传IFC文件时返回404:请求资源不存在

APS OSS v2 API上传IFC文件报404错误排查与解决

环境配置

  • ✅ 已通过2-legged OAuth(client_credentials)获取访问令牌,权限范围:data:read data:write bucket:create bucket:read viewables:read
  • ✅ 已创建存储桶:maikhanh-2024202524
  • ✅ 本地文件naman.ifc存在且可读

问题代码

import axios from 'axios';
import fs from 'fs';

const accessToken = '...';
const bucketKey = 'maikhanh-2024202524';
const objectName = 'naman.ifc';
const filePath = './naman.ifc';

const upload = async () => {
  const fileSize = fs.statSync(filePath).size;
  const fileStream = fs.createReadStream(filePath);

  await axios.put(
    `https://developer.api.autodesk.com/oss/v2/buckets/${bucketKey}/objects/${objectName}`,
    fileStream,
    {
      headers: {
        Authorization: `Bearer ${accessToken}`,
        'Content-Length': fileSize,
        'Content-Type': 'application/octet-stream'
      }
    }
  );
};

upload();

错误信息

{
  "developerMessage": "The requested resource does not exist.",
  "errorCode": "",
  "status": 404
}

排查与解决

404错误核心原因是请求的存储桶未被API识别,常见触发点及修复方案如下:

1. 存储桶区域不匹配

APS OSS存储桶是区域化的,若创建桶时指定了非默认区域(默认是US),上传请求必须明确指定区域,否则API会在默认区域查找桶,导致找不到资源。

修复方式二选一:

  • 在请求头中添加区域标识:
    headers: {
      // 其他头信息
      'x-ads-region': 'EMEA' // 替换为你的桶实际区域,如US、APAC等
    }
    
  • 使用区域专属API端点:
    例如EMEA区域的端点为https://oss-emea.autodesk.com/v2/buckets/${bucketKey}/objects/${objectName},替换原URL即可。

2. 桶名拼写错误

确认代码中的bucketKey与实际创建的桶名完全一致,APS桶名区分大小写,且仅允许小写字母、数字、连字符,若存在大小写不一致或字符错误,会导致API无法定位桶。

3. 大文件上传配置缺失

IFC文件通常体积较大,axios默认的内容长度限制可能导致上传异常,可添加以下配置避免:

{
  // 其他配置
  maxContentLength: Infinity,
  maxBodyLength: Infinity
}

修改后的完整代码示例

import axios from 'axios';
import fs from 'fs';

const accessToken = '...';
const bucketKey = 'maikhanh-2024202524';
const objectName = 'naman.ifc';
const filePath = './naman.ifc';
const bucketRegion = 'US'; // 替换为你的桶实际区域

const upload = async () => {
  const fileSize = fs.statSync(filePath).size;
  const fileStream = fs.createReadStream(filePath);

  try {
    const response = await axios.put(
      `https://developer.api.autodesk.com/oss/v2/buckets/${bucketKey}/objects/${objectName}`,
      fileStream,
      {
        headers: {
          Authorization: `Bearer ${accessToken}`,
          'Content-Length': fileSize,
          'Content-Type': 'application/octet-stream',
          'x-ads-region': bucketRegion
        },
        maxContentLength: Infinity,
        maxBodyLength: Infinity
      }
    );
    console.log('上传成功:', response.data);
  } catch (error) {
    console.error('上传失败:', error.response?.data || error.message);
  }
};

upload();

内容的提问来源于stack exchange,提问作者Archi Nam An

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 17:23:16