读取文件

更新时间:
复制 MD 格式

使用 GetItem 接口按稳定 itemId 或路径读取一个文件,两者必须且只能提供一个。

前提条件

  • 已创建 AgentStorage 实例且状态为 normal,并获取实例访问地址(endpoint)和实例名。

  • 已创建 API Key。

  • 文件操作使用精确 Scope,scope 的四级字段(appId、tenantId、agentId、runId)全部必填,不支持通配符 *。

  • 读取文件内容时设置 view=full。

请求参数

字段

类型

必填

说明

itemId

string

条件必填

文件稳定 ID,与 path 二选一

path

string

条件必填

文件路径,与 itemId 二选一

view

string

否

默认 basic;读取内容时传 full

请求示例

使用 API Key 认证时,通过 x-ots-instancename 和 x-ots-apikey 请求头传入实例名和 API Key。

curl -X POST https://<endpoint>/GetItem \
  -H "x-ots-instancename: <instance-name>" \
  -H "x-ots-apikey: <your-api-key>" \
  -H "Content-Type: application/json" \
  -d '{
  "type": "memoryfile",
  "memoryStoreName": "agent_files",
  "scope": {
    "appId": "app-001",
    "tenantId": "user-001",
    "agentId": "assistant",
    "runId": "session-001"
  },
  "path": "/profile/preferences.md",
  "view": "full"
}'

响应

文本文件在 view=full 时通过 content 返回正文。二进制文件在基础视图和完整视图中均返回 mediaType,仅在 view=full 时通过 contentBase64 返回标准 Base64 编码内容,不返回 content。

{
  "type": "memoryfile",
  "itemId": "mem_01M2Z...",
  "scope": {
    "appId": "app-001",
    "tenantId": "user-001",
    "agentId": "assistant",
    "runId": "run-001"
  },
  "path": "/profile/preferences.md",
  "content": "# 用户偏好\n\n- 喜欢美式咖啡\n",
  "contentSha256": "40556ec6...",
  "contentSizeBytes": 37,
  "latestVersionId": "fmver3...",
  "createdAt": "2026-09-20T09:03:24Z",
  "updatedAt": "2026-09-20T09:03:24Z"
}