镜像仓库
更新时间:2026-04-27
镜像仓库管理
前置代码
如下代码用于初始化 CCR Client,后续示例均基于该 ccrClient。文中的 instanceID、projectName 和 repositoryName 均为示例值,请按实际资源替换。
Go
1package main
2
3import (
4 "fmt"
5
6 "github.com/baidubce/bce-sdk-go/services/eccr"
7)
8
9func newClient() (*eccr.Client, error) {
10 ak := "<your-ak>"
11 sk := "<your-sk>"
12 endpoint := "ccr.bj.baidubce.com"
13 return eccr.NewClient(ak, sk, endpoint)
14}
15
16func main() {
17 ccrClient, err := newClient()
18 if err != nil {
19 fmt.Println("new eccr client failed:", err)
20 return
21 }
22
23 _ = ccrClient
24}
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
ak |
string |
是 | Access Key,占位符示例为 <your-ak>。 |
sk |
string |
是 | Secret Key,占位符示例为 <your-sk>。 |
endpoint |
string |
是 | CCR 服务地址。北京地域可使用 ccr.bj.baidubce.com。 |
以下各方法示例片段可直接放入前置代码中的 main 函数中使用。
ListRepositories
如下代码可以列举命名空间下镜像仓库:
Go
1instanceID := "ccr-xxxxxxxx"
2projectName := "demo-project"
3
4args := &eccr.ListRepositoriesArgs{
5 PageNo: 1,
6 PageSize: 10,
7 RepositoryName: "",
8}
9
10resp, err := ccrClient.ListRepositories(instanceID, projectName, args)
11if err != nil {
12 fmt.Println("list repositories failed:", err)
13 return
14}
15
16fmt.Println("total:", resp.Total)
17for _, repo := range resp.Items {
18 fmt.Printf("repository=%s tagCount=%d\n", repo.RepositoryName, repo.TagCount)
19}
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
instanceID |
string |
是 | CCR 企业版实例 ID。 |
projectName |
string |
是 | 命名空间名称。 |
args.PageNo |
int |
否 | 当前页码,通常从 1 开始。 |
args.PageSize |
int |
否 | 每页记录数。默认 10,最大 100。 |
args.RepositoryName |
string |
否 | 按仓库名称过滤。传空字符串表示不过滤。 |
返回值说明:
| 字段 | 类型 | 说明 |
|---|---|---|
resp.Total |
int |
仓库总数。 |
resp.PageNo |
int |
当前页码。 |
resp.PageSize |
int |
每页记录数。 |
resp.Items |
[]Repository |
当前页返回的镜像仓库列表。 |
repo.RepositoryName |
string |
镜像仓库名称。 |
repo.Description |
string |
镜像仓库描述。 |
repo.TagCount |
int |
当前仓库的 Tag 数量。 |
repo.PullCount |
int |
仓库被拉取的次数。 |
GetRepositoryDetail
如下代码可以查询镜像仓库详情:
Go
1instanceID := "ccr-xxxxxxxx"
2projectName := "demo-project"
3repositoryName := "nginx"
4
5resp, err := ccrClient.GetRepositoryDetail(instanceID, projectName, repositoryName)
6if err != nil {
7 fmt.Println("get repository detail failed:", err)
8 return
9}
10
11fmt.Println("repository:", resp.RepositoryName)
12fmt.Println("description:", resp.Description)
13fmt.Println("pull count:", resp.PullCount)
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
instanceID |
string |
是 | CCR 企业版实例 ID。 |
projectName |
string |
是 | 命名空间名称。 |
repositoryName |
string |
是 | 镜像仓库名称。若名称包含 /,建议先进行 Base64 编码,例如 lib/ubuntu 可编码为 bGliL3VidW50dQ==。 |
返回值说明:
| 字段 | 类型 | 说明 |
|---|---|---|
resp.ProjectName |
string |
命名空间名称。 |
resp.RepositoryName |
string |
镜像仓库名称。 |
resp.Description |
string |
镜像仓库描述。 |
resp.RepositoryPath |
string |
公网访问镜像路径。 |
resp.PrivateRepositoryPath |
string |
VPC 内访问镜像路径。 |
resp.TagCount |
int |
当前仓库的 Tag 数量。 |
resp.PullCount |
int |
仓库被拉取的次数。 |
resp.CreationTime |
string |
创建时间。 |
resp.UpdateTime |
string |
更新时间。 |
UpdateRepository
如下代码可以更新镜像仓库描述:
Go
1instanceID := "ccr-xxxxxxxx"
2projectName := "demo-project"
3repositoryName := "nginx"
4
5args := &eccr.UpdateRepositoryArgs{
6 Description: "updated by sdk example",
7}
8
9resp, err := ccrClient.UpdateRepository(instanceID, projectName, repositoryName, args)
10if err != nil {
11 fmt.Println("update repository failed:", err)
12 return
13}
14
15fmt.Println("updated repository:", resp.RepositoryName)
16fmt.Println("updated description:", resp.Description)
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
instanceID |
string |
是 | CCR 企业版实例 ID。 |
projectName |
string |
是 | 命名空间名称。 |
repositoryName |
string |
是 | 镜像仓库名称。若名称包含 /,建议先进行 Base64 编码,例如 lib/ubuntu 可编码为 bGliL3VidW50dQ==。 |
args.Description |
string |
是 | 更新后的镜像仓库描述。 |
返回值说明:
| 字段 | 类型 | 说明 |
|---|---|---|
resp.RepositoryName |
string |
更新后的镜像仓库名称。 |
resp.Description |
string |
更新后的镜像仓库描述。 |
resp.RepositoryPath |
string |
公网访问镜像路径。 |
resp.PrivateRepositoryPath |
string |
VPC 内访问镜像路径。 |
resp.TagCount |
int |
当前仓库的 Tag 数量。 |
resp.PullCount |
int |
仓库被拉取的次数。 |
resp.CreationTime |
string |
创建时间。 |
resp.UpdateTime |
string |
更新时间。 |
调用成功后,可通过 resp.Description 和 resp.UpdateTime 确认描述已生效。
DeleteRepository
如下代码可以删除镜像仓库:
Go
1instanceID := "ccr-xxxxxxxx"
2projectName := "demo-project"
3repositoryName := "repo-to-delete"
4
5err := ccrClient.DeleteRepository(instanceID, projectName, repositoryName)
6if err != nil {
7 fmt.Println("delete repository failed:", err)
8 return
9}
10
11fmt.Println("delete repository success")
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
instanceID |
string |
是 | CCR 企业版实例 ID。 |
projectName |
string |
是 | 命名空间名称。 |
repositoryName |
string |
是 | 待删除的镜像仓库名称。若名称包含 /,建议先进行 Base64 编码,例如 lib/ubuntu 可编码为 bGliL3VidW50dQ==。 |
返回值说明:该方法无返回值;当 err == nil 时表示删除成功。
BatchDeleteRepositories
如下代码可以批量删除镜像仓库:
Go
1instanceID := "ccr-xxxxxxxx"
2projectName := "demo-project"
3
4args := &eccr.BatchDeleteRepositoriesArgs{
5 Items: []string{"repo-a", "repo-b"},
6}
7
8err := ccrClient.BatchDeleteRepositories(instanceID, projectName, args)
9if err != nil {
10 fmt.Println("batch delete repositories failed:", err)
11 return
12}
13
14fmt.Println("batch delete repositories success")
参数说明:
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
instanceID |
string |
是 | CCR 企业版实例 ID。 |
projectName |
string |
是 | 命名空间名称。 |
args.Items |
[]string |
是 | 待删除的镜像仓库名称数组。 |
返回值说明:该方法无返回值;当 err == nil 时表示批量删除成功。
评价此篇文章
