上传文件
更新时间:2026-11-24
接口说明
- 本接口用于上传文件到账号文件区,上传成功后可挂载到会话沙箱作为对话附件。
- 请求体为 multipart/form-data。第一个 part 必须是字段名为 file 的文件,文件名取该 part 的 filename,后续 part 不读取。
- 单文件上限 100MB。每个账号最多保留 1000 个未删除的上传文件,超出返回 403 FileQuotaExceeded;单文件超限返回 413 FileTooLarge。
- 返回的 fileId 用于建会话时挂载(fileIds)或作为对话附件。
请求结构
Plain Text
1POST /v1/agents/files/upload HTTP/1.1
2Host: aw-harness.bj.baidubce.com
3Authorization: authorization string
4Content-Type: multipart/form-data; boundary=----Boundary
5
6------Boundary
7Content-Disposition: form-data; name="file"; filename="report.pdf"
8Content-Type: application/pdf
9
10<文件二进制内容>
11------Boundary--
请求头域
| 请求头 | 是否必需 | 描述 |
|---|---|---|
| Content-Type | 是 | 固定为 multipart/form-data,并携带 boundary。 |
请求参数
| 参数名称 | 类型 | 是否必需 | 参数位置 | 描述 |
|---|---|---|---|---|
| file | File | 是 | RequestBody参数 | 上传的文件。必须是第一个 part,字段名固定为 file,文件名取该 part 的 filename。单文件不超过 100MB。 |
返回头域
除公共头域,无其它特殊头域。
返回参数
返回新建文件的列表项。
| 参数名称 | 类型 | 描述 |
|---|---|---|
| fileId | String | 文件 ID,用于建会话时挂载或作为对话附件。 |
| name | String | 文件名。 |
| path | String | Agent 产物在沙箱内的路径。上传文件此字段为空。 |
| source | String | user_upload。 |
| mimeType | String | MIME 类型。 |
| sizeBytes | Long | 文件大小。 |
| agentId | String | 所属 Agent,上传文件此字段为空。 |
| sessionId | String | 所属会话,上传文件此字段为空。 |
| createTime | String | 创建时间,RFC3339 UTC。 |
| updateTime | String | 更新时间,RFC3339 UTC。 |
错误码
| 错误码 | 错误描述 | HTTP状态码 | 中文解释 |
|---|---|---|---|
| InvalidArgument | The request parameters are invalid. | 400 | 请求体不是合法 multipart,或第一个 part 字段名不是 file。 |
| Forbidden | Operation requires an admin caller. | 403 | 缺少鉴权账号或跨账号访问他人资源。 |
| FileQuotaExceeded | File quota exceeded. | 403 | 账号上传文件数已达上限,需删除不用的文件。 |
| FileTooLarge | The file is too large. | 413 | 上传文件超过 100MB 上限。 |
| PersistFailed | Failed to persist the file. | 500 | 服务端存储写失败,可重试。 |
| Internal | Internal server error. | 500 | 服务端内部错误,保留 requestId 联系维护方。 |
请求示例
Plain Text
1curl -X POST http://aw-harness.bj.baidubce.com/v1/agents/files/upload \
2 -H "Host: aw-harness.bj.baidubce.com" \
3 -H "Authorization: bce-auth-v1/f81d3b34e48048fbb2634dc7882d7e21/2015-08-11T04:17:29Z/3600/host/74c506f68c65e26c633bfa104c863fffac5190fdec1ec24b7c03eb5d67d2e1de" \
4 -F "file=@./report.pdf"
返回示例
Plain Text
1HTTP/1.1 200 OK
2Content-Type: application/json;charset=UTF-8
3x-bce-request-id: 9f3d0e6a-1c2b-4d5e-8a7f-0b1c2d3e4f5a
4Server: BWS
5
6{
7 "result": {
8 "fileId": "file-a1b2c3d4",
9 "name": "report.pdf",
10 "source": "user_upload",
11 "mimeType": "application/pdf",
12 "sizeBytes": 102400,
13 "createTime": "2026-11-19T08:00:00Z",
14 "updateTime": "2026-11-19T08:00:00Z"
15 },
16 "requestId": "9f3d0e6a-1c2b-4d5e-8a7f-0b1c2d3e4f5a"
17}
评价此篇文章
