v2-refactor-temp/docs/file-manager/files-api-research.md
调研时间:2026-04-18 资料来源:developers.openai.com、ai.google.dev、docs.claude.com(经 Context7 MCP / WebFetch 检索)
| 维度 | OpenAI | Google(Gemini Dev API) | Anthropic |
|---|---|---|---|
| 状态 | GA | GA | Beta(anthropic-beta: files-api-2025-04-14) |
| 单文件上限 | 512 MB | 2 GB | 500 MB |
| 账号/项目总容量 | 2.5 TB / 项目 | 20 GB / 项目 | 500 GB / 组织 |
| 生命周期 | 默认永久,可选 expires_after | 48 小时自动删除,不可续期 | 永久,需手动 DELETE |
| 存储计费 | 原始 Files 免费;Vector Store $0.10/GB·天 | 全免费 | 全免费 |
| 引用方式 | file_id / file_url / file_data(base64,≤32 MB) | file_data.file_uri / inline base64 | source.type="file" / base64 / url(统一 block) |
| purpose 分类 | 有(assistants/batch/fine-tune/vision/user_data/evals) | 无(单一池) | 无(单一池) |
| 跨能力复用 | 最强:Responses / Assistants / Batch / Fine-tune / Vision 共用 file_id | 主要服务 generateContent | Messages + Code Execution + Skills 双向闭环 |
| 特色能力 | Vector Store + File Search、Batch 产出回取 | 视频帧率 / 时间裁剪(fps/start_offset) | 与 Citations + Prompt Caching 深度协同、产出可下载 |
| 音视频原生 | 图像为主,音视频走专用模型 | ✅ 完整支持(MP4 / MP3 等) | ❌ 未明确支持,仅 PDF / 图像 / 文本 / 代码 |
| 多云可用 | N/A | Vertex AI 走 GCS URI,不用 Files API | Bedrock / Vertex AI 不可用(仅 Anthropic 第一方) |
file_id 在 Responses / Batch / Fine-tune / Vision 之间打通,purpose 强制分类带来秩序感。source 协议把 file 当作 base64 / url 的平替,与 Citations / Prompt Caching / Skills 深度联动,但仍在 beta 且不支持音视频。| 场景 | 推荐 | 理由 |
|---|---|---|
| 长期知识库 / 跨会话复用 | Anthropic / OpenAI | 均持久;避开 Google 的 48h 过期 |
| 视频 / 音频理解 | 几乎是唯一选择 | |
| 一份数据跑多条流水线(在线 + Batch + 微调) | OpenAI | purpose 体系最顺手 |
| 带高质量引用的文档问答 | Anthropic | document + citations.enabled + file_id 组合最舒服 |
| 预算敏感 | Google / Anthropic | 存储全免费;OpenAI 的 Vector Store 按 GB·天收费 |
Base URL https://api.openai.com,需 Authorization: Bearer $OPENAI_API_KEY。
| 操作 | 方法 | 路径 |
|---|---|---|
| 上传文件 | POST | /v1/files(multipart/form-data,字段 file + purpose;可选 expires_after) |
| 列出文件 | GET | /v1/files(可按 purpose / limit / order / after 过滤) |
| 获取元数据 | GET | /v1/files/{file_id} |
| 下载内容 | GET | /v1/files/{file_id}/content |
| 删除文件 | DELETE | /v1/files/{file_id} |
File 对象典型字段:id / object / bytes / created_at / expires_at / filename / purpose / status / status_details。
assistants:供 Assistants API(code_interpreter、file_search)引用。batch:Batch API .jsonl 输入;产出 purpose 为 batch_output。fine-tune:微调训练集(.jsonl,符合 chat/completion 格式)。vision:Vision / Responses 图像输入(png/jpg/gif/webp)。user_data:Responses API 通用用户文档(PDF 等),也可作为 Prompt 模板变量。evals:Evals API 数据集(细节未完全确认)。batch_output、fine-tune-results(不可手动上传)。input_file(PDF 等)。expires_at: null,文件永久保留,需手动 DELETE。expires_after(相对 created_at 的秒数 anchor),到期自动删除。input_file { file_id } / input_image { file_id };也支持 file_data(base64 ≤32 MB)与 file_url。file_search 工具走 Vector Store 消费 purpose=assistants 的文件;code_interpreter 通过 message attachments 引用。v1 已弃用。purpose=batch 的 .jsonl,在 /v1/batches 用 input_file_id 引用,输出通过 /v1/files/{output_file_id}/content 下载。purpose=fine-tune 上传训练 / 验证集。purpose=vision,可做视觉模型输入或 image edit 源图。from openai import OpenAI
client = OpenAI()
f = client.files.create(file=open("report.pdf", "rb"), purpose="user_data")
resp = client.responses.create(
model="gpt-5",
input=[{"role": "user", "content": [
{"type": "input_text", "text": "总结这份 PDF"},
{"type": "input_file", "file_id": f.id},
]}],
)
print(resp.output_text)
import fs from "fs";
import OpenAI from "openai";
const openai = new OpenAI();
const f = await openai.files.create({
file: fs.createReadStream("report.pdf"),
purpose: "user_data",
});
const r = await openai.responses.create({
model: "gpt-5",
input: [{ role: "user", content: [
{ type: "input_text", text: "总结这份 PDF" },
{ type: "input_file", file_id: f.id },
]}],
});
console.log(r.output_text);
file_id 可在 Responses / Assistants / Batch / Fine-tune / Vision 之间跨场景引用(受 purpose 约束),这是相较 Anthropic 与 Google 较少见的"长生命周期 + 跨能力"设计。file_id(Files API)/ file_url(外链)/ file_data(base64 内联 ≤32 MB),开发者在"持久托管"与"一次性内联"之间可灵活选择。evals purpose 的完整字段约束。expires_after 的最大 / 最小秒数上限。聚焦 Gemini Developer API(generativelanguage.googleapis.com)下的 Files service;末尾对比 Vertex AI。
基址:https://generativelanguage.googleapis.com
| 操作 | 方法 | 路径 |
|---|---|---|
| 媒体上传(resumable) | POST | /upload/v1beta/files |
| 仅创建元数据 | POST | /v1beta/files |
| 列出文件 | GET | /v1beta/files(pageSize ≤ 100,默认 10) |
| 获取文件 | GET | /v1beta/files/{name} |
| 删除文件 | DELETE | /v1beta/files/{name} |
| 注册 GCS 对象 | POST | /v1beta/files:register |
文件资源字段:name / displayName / mimeType / sizeBytes / uri / state(PROCESSING / ACTIVE / FAILED)/ expirationTime / sha256Hash / videoMetadata。
X-Goog-Upload-Protocol: resumable + X-Goog-Upload-Command: start/upload, finalize 头。SDK files.upload 底层即此。inlineData):字节直接放进 generateContent.contents。image/png、image/jpeg、image/webp、image/heic、image/heif。generateContent 请求总 payload ≤ 100 MB。expirationTime 字段标注过期时刻。在 contents.parts 中通过 file_data 引用:
{"file_data": {"mime_type": "video/mp4", "file_uri": "files/abc-123"}}
视频特殊点:
state 先为 PROCESSING,需轮询至 ACTIVE 才能推理。videoMetadata 可传 fps / start_offset / end_offset;Prompt 内用 MM:SS 引用时间戳。google-genai)from google import genai
client = genai.Client(api_key="YOUR_KEY")
my_file = client.files.upload(file="sample.pdf")
resp = client.models.generate_content(
model="gemini-2.5-pro",
contents=["请总结这份文档", my_file],
)
print(resp.text)
for f in client.files.list():
print(f.name, f.state)
client.files.delete(name=my_file.name)
import { GoogleGenAI, createUserContent, createPartFromUri } from "@google/genai";
const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });
const uploaded = await ai.files.upload({
file: "sample.mp3",
config: { mimeType: "audio/mpeg" },
});
const resp = await ai.models.generateContent({
model: "gemini-2.5-flash",
contents: createUserContent([
"转写这段音频",
createPartFromUri(uploaded.uri, uploaded.mimeType),
]),
});
console.log(resp.text);
旧的
google-generativeai已进入维护模式;新项目请用google-genai(Python)/@google/genai(Node)。
Vertex AI 没有独立 Files API,文件引用通过:
gs:// URI(主流方式):公开可读或同项目。fileData。因此生产上 Vertex AI 侧的"Files"等价于 GCS 生命周期管理(用户自管 TTL、权限、计费),不存在 48 小时自动过期;Gemini Developer API 的 Files 则是托管式、免费但 48h 即焚的临时存储。
Files API 仍处于 beta 阶段,未 GA。调用任一端点需携带:
anthropic-beta: files-api-2025-04-14
anthropic-version: 2023-06-01
Messages 中引用 file_id 时同样需要此 beta header。不适用于 ZDR,且在 Amazon Bedrock / Google Vertex AI 上不可用。
| 方法 | 路径 | 说明 |
|---|---|---|
POST | /v1/files | 上传(multipart/form-data,字段 file) |
GET | /v1/files | 分页列出当前 workspace 的文件 |
GET | /v1/files/{file_id} | 获取元数据(id / filename / mime_type / size_bytes / created_at / type / downloadable,可选 scope) |
GET | /v1/files/{file_id}/content | 下载文件内容(仅限 Skills / Code Execution 产出;用户上传文件不可回取) |
DELETE | /v1/files/{file_id} | 删除(不可恢复) |
application/pdf → document blocktext/plain → document blockimage/jpeg / image/png / image/gif / image/webp → image blockcontainer_upload blockdocument 直传的格式(.csv / .md / .docx / .xlsx)建议先转 PDF 或纯文本。DELETE。expires_after、Google 48h TTL 不同)。source.type = "file" 替代 base64 / url:
{ "type": "document",
"source": { "type": "file", "file_id": "file_011C..." },
"title": "...", "context": "...",
"citations": { "enabled": true } }
{ "type": "image",
"source": { "type": "file", "file_id": "file_011C..." } }
file_id 哈希稳定,命中率高)。/content 下载。file_id(截图走 tool_result 的 image block),是否有直接消费路径未确认。cache_control 的 block,配合稳定 file_id,可大幅降低重复请求的 token 成本。from anthropic import Anthropic
client = Anthropic()
up = client.beta.files.upload(
file=("doc.pdf", open("doc.pdf", "rb"), "application/pdf"),
)
resp = client.beta.messages.create(
model="claude-opus-4-7", max_tokens=1024,
betas=["files-api-2025-04-14"],
messages=[{"role": "user", "content": [
{"type": "text", "text": "总结这份文档"},
{"type": "document", "source": {"type": "file", "file_id": up.id}},
]}],
)
import Anthropic, { toFile } from "@anthropic-ai/sdk";
import fs from "fs";
const anthropic = new Anthropic();
const up = await anthropic.beta.files.upload({
file: await toFile(fs.createReadStream("doc.pdf"), undefined, { type: "application/pdf" }),
betas: ["files-api-2025-04-14"],
});
const resp = await anthropic.beta.messages.create({
model: "claude-opus-4-7", max_tokens: 1024, betas: ["files-api-2025-04-14"],
messages: [{ role: "user", content: [
{ type: "text", text: "总结这份文档" },
{ type: "document", source: { type: "file", file_id: up.id } },
]}],
});
source 协议把 base64 / url / file 视为可互换子类型,前端代码几乎无需改动即可从"嵌入式"升级为"引用式";比 OpenAI 的 file_id 与 image_url 混合模型更一致。file_id 回流并可 /content 下载——形成"上传 → 分析 → 产出 → 下载"闭环;OpenAI 的 Code Interpreter 回取需走 assistants/thread 链路,路径更长。document block 内建 citations.enabled,配合 file_id 产生稳定字符 / 页码级引用;Prompt Caching 命中率相比重复 base64 显著更高。file_id。