Skip to main content
REST API 现已经过版本控制。 有关详细信息,请参阅“关于 API 版本控制”。

Git Blob 的 REST API 终结点

使用 REST API 与 Git blob(二进制大型对象)交互,该对象类型用于将每个文件的内容存储在存储库中。

关于 Git Blob

Git Blob(二进制大对象)是用于将每个文件的内容存储在仓库中的对象类型。 文件的 SHA-1 哈希在 Blob 对象中计算和存储。 通过这些终结点,可以在 GitHub Enterprise Server 上读取 blob 对象并将其写入 Git 数据库。 Blob 利用这些自定义媒体类型。 有关 API 中的媒体类型使用的详细信息,请参阅“REST API 入门”。

Create a blob

“Create a blob”的细粒度访问令牌

此端点支持以下精细令牌类型:

精细令牌必须具有以下权限集:

  • "Contents" repository permissions (write)

“Create a blob”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
owner string 必须

The account owner of the repository. The name is not case sensitive.

repo string 必须

The name of the repository without the .git extension. The name is not case sensitive.

正文参数
名称, 类型, 说明
content string 必须

The new blob's content.

encoding string

The encoding used for content. Currently, "utf-8" and "base64" are supported.

默认: utf-8

“Create a blob”的 HTTP 响应状态代码

状态代码说明
201

Created

403

Forbidden

404

Resource not found

409

Conflict

422

Validation failed

“Create a blob”的示例代码

请求示例

post/repos/{owner}/{repo}/git/blobs
curl -L \ -X POST \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ http(s)://HOSTNAME/api/v3/repos/OWNER/REPO/git/blobs \ -d '{"content":"Content of the blob","encoding":"utf-8"}'

Response

Status: 201
{ "url": "https://HOSTNAME/repos/octocat/example/git/blobs/3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15", "sha": "3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15" }

Get a blob

The content in the response will always be Base64 encoded.

This endpoint supports the following custom media types. For more information, see "Media types."

  • application/vnd.github.raw+json: Returns the raw blob data.
  • application/vnd.github+json: Returns a JSON representation of the blob with content as a base64 encoded string. This is the default if no media type is specified.

Note This endpoint supports blobs up to 100 megabytes in size.

“Get a blob”的细粒度访问令牌

此端点支持以下精细令牌类型:

精细令牌必须具有以下权限集:

  • "Contents" repository permissions (read)

如果仅请求公共资源,则无需身份验证或上述权限即可使用此终结点。

“Get a blob”的参数

标头
名称, 类型, 说明
accept string

Setting to application/vnd.github+json is recommended.

路径参数
名称, 类型, 说明
owner string 必须

The account owner of the repository. The name is not case sensitive.

repo string 必须

The name of the repository without the .git extension. The name is not case sensitive.

file_sha string 必须

“Get a blob”的 HTTP 响应状态代码

状态代码说明
200

OK

403

Forbidden

404

Resource not found

409

Conflict

422

Validation failed, or the endpoint has been spammed.

“Get a blob”的示例代码

请求示例

get/repos/{owner}/{repo}/git/blobs/{file_sha}
curl -L \ -H "Accept: application/vnd.github+json" \ -H "Authorization: Bearer <YOUR-TOKEN>" \ -H "X-GitHub-Api-Version: 2022-11-28" \ http(s)://HOSTNAME/api/v3/repos/OWNER/REPO/git/blobs/FILE_SHA

Response

Status: 200
{ "content": "Q29udGVudCBvZiB0aGUgYmxvYg==", "encoding": "base64", "url": "https://HOSTNAME/repos/octocat/example/git/blobs/3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15", "sha": "3a0f86fb8db8eea7ccbb9a95f325ddbedfb25e15", "size": 19, "node_id": "Q29udGVudCBvZiB0aGUgYmxvYg==" }