You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

使用Autodesk API上传CAD文件时持续出现403错误求助

Autodesk OSS文件上传HTTP 403错误排查与修复

HTTP 403错误通常是权限或请求配置问题导致的,针对你的场景,可从以下几个方向排查:

1. 令牌权限缺失

  • 确认获取Token时请求的scope包含data:write权限,上传文件需要该权限,仅bucket:create权限不足以完成文件写入。
  • 检查Token是否过期,可通过Autodesk API的令牌验证接口确认有效性。

2. 存储桶与文件路径配置错误

  • 确保mycompany-cadbucket-test存储桶是当前Token所属账户创建的,OSS存储桶是账户隔离的,无法上传至其他账户的桶。
  • 代码中定义了safe_file_name = quote(FILE_NAME)但未在URL中使用,硬编码的CADAPICHECK.stp可能与实际文件名不符,应替换为变量:
    url = f'https://developer.api.emea.autodesk.com/oss/v2/buckets/mycompany-cadbucket-test/objects/{safe_file_name}'
    

3. 请求端点与存储桶区域不匹配

  • 代码使用EMEA区域端点developer.api.emea.autodesk.com,需确认存储桶是在EMEA区域创建的。若桶创建在其他区域(如美国),需切换为对应端点developer.api.autodesk.com

4. 请求负载与头信息问题

  • 大文件建议直接用文件对象作为data参数(而非读取全部内容到内存),避免内存溢出或传输中断:
    with open(FILE_PATH, 'rb') as f:
        response = requests.put(url, headers=headers, data=f)
    
  • 确认Content-Type设置正确,application/octet-stream兼容所有二进制文件,也可根据CAD类型使用更具体的类型(如application/vnd.step)。

修正后的示例代码

# 4. Upload File
# --------------------
def upload_file(token):
    safe_file_name = quote(FILE_NAME)
    url = f'https://developer.api.emea.autodesk.com/oss/v2/buckets/mycompany-cadbucket-test/objects/{safe_file_name}'
    headers = {
        'Authorization': f'Bearer {token}',
        'Content-Type': 'application/octet-stream'
    }
    with open(FILE_PATH, 'rb') as f:
        response = requests.put(url, headers=headers, data=f)
    print(f"📤 Uploading to: {url}")
    print("🔍 Upload response:", response.text)
    response.raise_for_status()
    object_id = response.json()['objectId']
    print(f"✅ File uploaded. Object ID: {object_id}")
    return object_id

内容的提问来源于stack exchange,提问作者Skymeric Sales

火山引擎 最新活动