# 文件与素材

视频、图像接口需要公网可访问的素材 URL。本地文件可以先用本接口上传，拿到平台生成的 `file_*` ID 和 `url`，再把 URL 放入生成请求。

## 接口一览

| 方法 | 路径 | 说明 |
| --- | --- | --- |
| `POST` | `/v1/files` | 上传素材 |
| `GET` | `/v1/files/{file_id}` | 查询素材信息与 URL |
| `GET` | `/v1/files/{file_id}/content` | 下载素材（307 重定向） |
| `POST` | `/api/v3/files/uploads` | NoToken 兼容上传 |
| `GET` | `/api/v3/files/{file_id}` | NoToken 兼容查询 |

所有接口使用 `Authorization: Bearer sk-xxxxxxxx`。

## 上传素材

**接口地址**：`POST https://YOUR_API_BASE_URL/v1/files`

- 鉴权：Bearer Token
- Content-Type：multipart/form-data

必填文件字段 file；可选 purpose，省略时为 vision。

### 请求参数

| 字段 | 类型 | 必填 | 说明 |
| --- | --- | --- | --- |
| `file` | file | 是 | 图片、视频或音频文件，单个最大 100 MiB |
| `purpose` | string | 否 | 用途，默认 `vision`，最长 64 个字符 |

服务端根据文件内容识别类型，只接受图片、视频或音频。伪装扩展名的文件会返回 `415`。

### 请求示例

```bash
curl -X POST "https://YOUR_API_BASE_URL/v1/files" \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  -F "file=@./person.png" \
  -F "purpose=vision"
```

Python：

```python
import os
import requests

with open("person.png", "rb") as f:
    resp = requests.post(
        os.environ["API_BASE"] + "/v1/files",
        headers={"Authorization": f"Bearer {os.environ['API_KEY']}"},
        files={"file": ("person.png", f, "image/png")},
        data={"purpose": "vision"},
    )
print(resp.json()["id"])
```

### 上传响应

```json
{
  "id": "file_a1b2c3d4",
  "object": "file",
  "filename": "person.png",
  "purpose": "vision",
  "bytes": 248102,
  "created_at": 1781180000
}
```

| 字段 | 说明 |
| --- | --- |
| `id` | 平台文件 ID，用于查询与下载 |
| `object` | 固定为 `file` |
| `filename` | 原始文件名 |
| `purpose` | 用途 |
| `bytes` | 文件大小 |
| `created_at` | Unix 秒时间戳 |

**提示**

上传响应**不包含**可直接引用的 URL。需要再调用一次查询接口获取 `url`。

## 查询素材

**接口地址**：`GET https://YOUR_API_BASE_URL/v1/files/{file_id}`

- 鉴权：Bearer Token

返回素材元信息与可访问的 url。

```bash
curl "https://YOUR_API_BASE_URL/v1/files/file_a1b2c3d4" \
  -H "Authorization: Bearer sk-xxxxxxxx"
```

```json
{
  "id": "file_a1b2c3d4",
  "object": "file",
  "filename": "person.png",
  "purpose": "vision",
  "bytes": 248102,
  "created_at": 1781180000,
  "url": "https://media.example.com/media/uploads/2026/08/file_a1b2c3d4.png"
}
```

`url` 的形态取决于服务配置：

- 配置了公开访问域名时，返回稳定的公共地址；
- 未配置时，返回带有效期的预签名地址。

预签名地址会过期，请在有效期内使用，或重新查询获取新地址。

## 下载素材

```http
GET /v1/files/{file_id}/content
```

返回 `307` 重定向到与查询接口相同的下载地址。使用 cURL 时加 `-L`：

```bash
curl -L "https://YOUR_API_BASE_URL/v1/files/file_a1b2c3d4/content" \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  --output person.png
```

## 在生成接口中使用

拿到 `url` 后，放入视频或图像的素材字段：

```json
{
  "model": "YOUR_VIDEO_MODEL",
  "content": [
    { "type": "text", "text": "让 @image1 中的人物转身。" },
    {
      "type": "image_url",
      "role": "reference_image",
      "image_url": { "url": "https://media.example.com/media/uploads/2026/08/file_a1b2c3d4.png" }
    }
  ],
  "duration": 5,
  "ratio": "16:9",
  "resolution": "720p"
}
```

图像编辑也可以直接使用该 URL：

```json
{
  "model": "YOUR_IMAGE_MODEL",
  "prompt": "保持人物，将背景改为海边",
  "image": "https://media.example.com/media/uploads/2026/08/file_a1b2c3d4.png"
}
```

## NoToken 兼容接口

部分旧客户端使用 `/api/v3/files` 路径，行为与 `/v1/files` 基本一致：

```bash
curl -X POST "https://YOUR_API_BASE_URL/api/v3/files/uploads" \
  -H "Authorization: Bearer sk-xxxxxxxx" \
  -F "file=@./person.png"
```

差异：

- 只接受 `file` 字段；
- 上传与查询响应不包含 `purpose`；
- 查询路径为 `GET /api/v3/files/{file_id}`。

## 隔离与安全

- 文件按 API Key 所属用户隔离。其他用户的 `file_id` 无法查询或下载。
- 不要把 `file_id` 当作公网 URL 传给服务；生成接口需要的是查询返回的 `url`。
- 素材可能包含隐私信息，请勿上传敏感内容；必要时在业务侧做脱敏。
- 平台会在内部完成素材登记，调用方不需要传 `asset://` 之类的内部标识。

## 错误

| 状态码 | 含义 |
| --- | --- |
| `400` | 缺少 `file`、`purpose` 过长、表单解析失败 |
| `401` | Key 无效 |
| `404` | 文件不存在或不属于当前 Key |
| `413` | 文件超过大小限制 |
| `415` | 文件类型不受支持 |
| `429` | 上传频率超限 |

## 相关阅读

- [视频生成](https://docs.myaigf.cfd/api/videos)
- [图像生成与编辑总览](https://docs.myaigf.cfd/api/images)
