OpenAI 容器文件资源与对象

2026-11-12 14:59 更新

容器文件

创建容器文件

post /containers/{container_id}/files

创建容器文件。

可以发送包含原始文件内容的 multipart/form-data 请求,也可以发送包含文件 ID 的 JSON 请求。

路径参数

  • container_id: string

请求体参数

内容类型:application/json

  • file: optional string

要上传的 File 对象,而不是文件名。

  • file_id: optional string

要创建的文件的名称。译注:原文如此,但此处参数名为 file_id,通过 JSON 引用已有文件时应传文件 ID,不应传文件名。

返回值

  • id: string

文件的唯一标识符。

  • bytes: integer

文件大小,单位为字节。

  • container_id: string

此文件所属的容器。

  • created_at: integer

创建文件时的 Unix 时间戳,单位为秒。

format: unixtime

  • object: string

此对象的类型(container.file)。

  • path: string

文件在容器中的路径。

  • source: string

文件的来源,例如 user(用户)或 assistant(助手)。

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/$CONTAINER_ID/files \
    -H 'Content-Type: application/json' \
    -H "Authorization: Bearer $OPENAI_API_KEY" \
    -d '{}'

200 响应示例

{
  "id": "id",
  "bytes": 0,
  "container_id": "container_id",
  "created_at": 0,
  "object": "object",
  "path": "path",
  "source": "source"
}

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/cntr_682e0e7318108198aa783fd921ff305e08e78805b9fdbb04/files \
  -H "Authorization: Bearer $OPENAI_API_KEY" \
  -F file="@example.txt"

响应示例

{
  "id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
  "object": "container.file",
  "created_at": 1747848842,
  "bytes": 880,
  "container_id": "cntr_682e0e7318108198aa783fd921ff305e08e78805b9fdbb04",
  "path": "/mnt/data/88e12fa445d32636f190a0b33daed6cb-tsconfig.json",
  "source": "user"
}

删除容器文件

delete /containers/{container_id}/files/{file_id}

删除容器文件。

路径参数

  • container_id: string

  • file_id: string

返回值

  • id: string

  • deleted: boolean

  • object: "container.file.deleted"

  • "container.file.deleted"

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/$CONTAINER_ID/files/$FILE_ID \
    -X DELETE \
    -H "Authorization: Bearer $OPENAI_API_KEY"

200 响应示例

{
  "id": "id",
  "deleted": true,
  "object": "container.file.deleted"
}

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl -X DELETE https://api.openai.com/v1/containers/cntr_682dfebaacac8198bbfe9c2474fb6f4a085685cbe3cb5863/files/cfile_682e0e8a43c88191a7978f477a09bdf5 \
  -H "Authorization: Bearer $OPENAI_API_KEY"

响应示例

{
    "id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
    "object": "container.file.deleted",
    "deleted": true
}

列出容器文件

get /containers/{container_id}/files

列出容器文件。

路径参数

  • container_id: string

查询参数

  • after: optional string

用于分页的游标。after 是用于标记列表位置的对象 ID。例如,列表请求返回 100 个对象,最后一个对象的 ID 为 obj_foo,则后续调用可传入 after=obj_foo 来获取下一页。

  • limit: optional integer (default: 20)

要返回的对象数量上限,取值范围为 1 至 100,默认为 20。

  • order: optional "asc" or "desc" (default: "desc")

按对象的 created_at 时间戳排序。asc 为升序,desc 为降序。

  • "asc"

  • "desc"

返回值

  • data: array of object { id, bytes, container_id, 4 more }

容器文件列表。

  • id: string

文件的唯一标识符。

  • bytes: integer

文件大小,单位为字节。

  • container_id: string

此文件所属的容器。

  • created_at: integer

创建文件时的 Unix 时间戳,单位为秒。

format: unixtime

  • object: string

此对象的类型(container.file)。

  • path: string

文件在容器中的路径。

  • source: string

文件的来源,例如 user(用户)或 assistant(助手)。

  • first_id: string

列表中第一个文件的 ID。

  • has_more: boolean

是否还有更多文件可用。

  • last_id: string

列表中最后一个文件的 ID。

  • object: "list"

返回对象的类型,必须为 list。

  • "list"

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/$CONTAINER_ID/files \
    -H "Authorization: Bearer $OPENAI_API_KEY"

200 响应示例

{
  "data": [
    {
      "id": "id",
      "bytes": 0,
      "container_id": "container_id",
      "created_at": 0,
      "object": "object",
      "path": "path",
      "source": "source"
    }
  ],
  "first_id": "first_id",
  "has_more": true,
  "last_id": "last_id",
  "object": "list"
}

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/cntr_682e0e7318108198aa783fd921ff305e08e78805b9fdbb04/files \
  -H "Authorization: Bearer $OPENAI_API_KEY"

响应示例

{
    "object": "list",
    "data": [
        {
            "id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
            "object": "container.file",
            "created_at": 1747848842,
            "bytes": 880,
            "container_id": "cntr_682e0e7318108198aa783fd921ff305e08e78805b9fdbb04",
            "path": "/mnt/data/88e12fa445d32636f190a0b33daed6cb-tsconfig.json",
            "source": "user"
        }
    ],
    "first_id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
    "has_more": false,
    "last_id": "cfile_682e0e8a43c88191a7978f477a09bdf5"
}

检索容器文件

get /containers/{container_id}/files/{file_id}

检索容器文件。

路径参数

  • container_id: string

  • file_id: string

返回值

  • id: string

文件的唯一标识符。

  • bytes: integer

文件大小,单位为字节。

  • container_id: string

此文件所属的容器。

  • created_at: integer

创建文件时的 Unix 时间戳,单位为秒。

format: unixtime

  • object: string

此对象的类型(container.file)。

  • path: string

文件在容器中的路径。

  • source: string

文件的来源,例如 user(用户)或 assistant(助手)。

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/$CONTAINER_ID/files/$FILE_ID \
    -H "Authorization: Bearer $OPENAI_API_KEY"

200 响应示例

{
  "id": "id",
  "bytes": 0,
  "container_id": "container_id",
  "created_at": 0,
  "object": "object",
  "path": "path",
  "source": "source"
}

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/container_123/files/file_456 \
  -H "Authorization: Bearer $OPENAI_API_KEY"

响应示例

{
    "id": "cfile_682e0e8a43c88191a7978f477a09bdf5",
    "object": "container.file",
    "created_at": 1747848842,
    "bytes": 880,
    "container_id": "cntr_682e0e7318108198aa783fd921ff305e08e78805b9fdbb04",
    "path": "/mnt/data/88e12fa445d32636f190a0b33daed6cb-tsconfig.json",
    "source": "user"
}

领域类型

创建文件响应

  • FileCreateResponse object { id, bytes, container_id, 4 more }

  • id: string

文件的唯一标识符。

  • bytes: integer

文件大小,单位为字节。

  • container_id: string

此文件所属的容器。

  • created_at: integer

创建文件时的 Unix 时间戳,单位为秒。

format: unixtime

  • object: string

此对象的类型(container.file)。

  • path: string

文件在容器中的路径。

  • source: string

文件的来源,例如 user(用户)或 assistant(助手)。

删除文件响应

  • FileDeleteResponse object { id, deleted, object }

  • id: string

  • deleted: boolean

  • object: "container.file.deleted"

  • "container.file.deleted"

列出文件响应

  • FileListResponse object { id, bytes, container_id, 4 more }

  • id: string

文件的唯一标识符。

  • bytes: integer

文件大小,单位为字节。

  • container_id: string

此文件所属的容器。

  • created_at: integer

创建文件时的 Unix 时间戳,单位为秒。

format: unixtime

  • object: string

此对象的类型(container.file)。

  • path: string

文件在容器中的路径。

  • source: string

文件的来源,例如 user(用户)或 assistant(助手)。

检索文件响应

  • FileRetrieveResponse object { id, bytes, container_id, 4 more }

  • id: string

文件的唯一标识符。

  • bytes: integer

文件大小,单位为字节。

  • container_id: string

此文件所属的容器。

  • created_at: integer

创建文件时的 Unix 时间戳,单位为秒。

format: unixtime

  • object: string

此对象的类型(container.file)。

  • path: string

文件在容器中的路径。

  • source: string

文件的来源,例如 user(用户)或 assistant(助手)。

文件内容

检索容器文件内容

get /containers/{container_id}/files/{file_id}/content

检索容器文件内容。

路径参数

  • container_id: string

  • file_id: string

返回值

file

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/$CONTAINER_ID/files/$FILE_ID/content \
    -H "Authorization: Bearer $OPENAI_API_KEY"

示例

## 从环境变量读取 API 密钥;运行前替换容器、文件和技能等示例 ID。
curl https://api.openai.com/v1/containers/container_123/files/cfile_456/content \
  -H "Authorization: Bearer $OPENAI_API_KEY"

响应示例

(文件的二进制内容)
以上内容是否对您有帮助:
在线笔记
App下载
App下载

扫描二维码

下载编程狮App

公众号
微信公众号

编程狮公众号