Image
Updated at:2025-10-20
Create a custom image
- This API allows users to create custom images, with a default limit of 20 images per account per region. These custom images can then be used to create instances.
- Two methods are supported for creating custom images: via an instance or via a snapshot.
- When creating via an instance, the operation will only succeed if the instance is in either Running or Stopped status; otherwise, a 409 error will occur.
- Custom images can only be generated from system disk snapshots.
- When creating via a snapshot, the operation can only succeed if the snapshot is in Available status; otherwise, a 409 error will be returned
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 // Name of custom image to be created, which supports uppercase and lowercase letters, numbers, Chinese characters, and -_/special characters, starting with a letter and a length of 1-65.
15 imageName := "image02"
16 // When creating an image from a snapshot, this parameter refers to the ID of the snapshot used to create the image. Optional; it cannot coexist with snapshotId. If both exist, only instanceId will be used
17 snapshotId := "m-***"
18 // When creating an image from an instance, this parameter refers to the ID of the instance used to create the image. Optional; it cannot coexist with snapshotId. If both exist, only instanceId will be used
19 instanceId := "i-***"
20 // Whether to associate with CDS. Default: false
21 relateCds := false
22 args := &api.CreateImageArgs{
23 SnapshotId: snapshotId,
24 IsRelateCds: relateCds,
25 ImageName: imageName,
26 InstanceId: instanceId,
27 }
28 resp, err := bccClient.CreateImage(args)
29 fmt.Println(resp, err)
30}
Keep in mind that the default limit for creating custom images is 20 per account per region.
Query the image list
- Use the following code to query the list of accessible images based on your permissions.
- The returned image information includes public images, custom images, and service integration images.
- Filters queries using the imageType parameter, which is optional. The default value "All" queries all image types.
- It supports filtering queries by imageName, returning images that contain the specified string in their names. This parameter is optional; however, when setting imageName, you must also specify ImageType as 'Custom'.
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 createInstanceArgs := &api.ListImageArgs{
15 // Image type
16 ImageType: "ALL",
17 }
18 images, err := bccClient.ListImage(createInstanceArgs)
19 fmt.Println(err)
20 fmt.Println(images)
21}
For specific image types, refer to the BCC API documentation Query the Image List
Query image details
You can query image details with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5)
6func main() {
7 // Set your AK, SK and the endpoint to be accessed
8 ak := "ak"
9 sk := "sk"
10 endpoint := "http://bcc.bj.baidubce.com"
11 // Create a BCC client
12 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
13 imageId := "m-***"
14 imageDetail, err := bccClient.GetImageDetail(imageId)
15 fmt.Println(err)
16 fmt.Println(imageDetail.Image)
17}
Delete a custom image
- This API allows users to delete specified custom images. Only custom images can be deleted. Public images and service integration images cannot be deleted.
- If the image ID specified in imageId does not exist, a 404 error is returned.
- Once an image is deleted, it cannot be recovered or used to create or reset instances again.
You can delete a custom image with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5)
6func main() {
7 // Set your AK, SK and the endpoint to be accessed
8 ak := "ak"
9 sk := "sk"
10 endpoint := "http://bcc.bj.baidubce.com"
11 // Create a BCC client
12 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
13 imageId := "m-***"
14 err := bccClient.DeleteImage(imageId)
15 fmt.Println(err)
16}
Replicate a custom image across regions
- This feature allows users to replicate custom images across regions. Only custom images can be replicated. Public images and service integration images cannot be replicated.
- Regions such as Beijing "bj", Guangzhou "gz", Suzhou "su"; multiple selections are allowed:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 imageId := "m-***"
15 destinations := &api.RemoteCopyImageArgs{
16 Name: "new-image",
17 DestRegion: []string{"hkg"},
18 }
19 err := bccClient.RemoteCopyImage(imageId, destinations)
20 fmt.Println(err)
21}
Replicate a custom image across regions and return the image ID in the destination region
- This feature allows users to replicate custom images across regions. Only custom images can be replicated. Public images and service integration images cannot be replicated.
- Regions such as Beijing "bj", Guangzhou "gz", Suzhou "su"; multiple selections are allowed:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 imageId := "m-***"
15 destinations := &api.RemoteCopyImageArgs{
16 Name: "new-image",
17 DestRegion: []string{"hkg"},
18 }
19 imageCopyResult, err := bccClient.RemoteCopyImageReturnImageIds(imageId, destinations)
20 fmt.Println(err)
21 fmt.Println(imageCopyResult)
22}
Cancel cross-region replication of custom images
You can cancel the cross-region replication of a custom image with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5)
6func main() {
7 // Set your AK, SK and the endpoint to be accessed
8 ak := "ak"
9 sk := "sk"
10 endpoint := "http://bcc.bj.baidubce.com"
11 // Create a BCC client
12 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
13 imageId := "m-***"
14 err := bccClient.DeleteImage(imageId)
15 fmt.Println(err)
16}
Share a custom image
- This API enables users to share their specified custom images. Only custom images can be shared. Public images and service integration images cannot be shared.
- If the image ID specified in imageId does not exist, a 404 error is returned.
- Once an image is shared, the recipient can use it to create or reset instances.
- In the request, the account and accountId parameters are optional but at least one must be provided. If both are included, duplicates will automatically be removed by the server.
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 ShareImageId := "m-***"
15 // Request to share the image with a specified user ID
16 args := &api.SharedUser{
17 // Account ID
18 AccountId: "***",
19 }
20 err := bccClient.ShareImage(ShareImageId, args)
21 fmt.Println(err)
22}
Unshare a custom image
- This API is for users to stop sharing specified custom images. Only custom images can be unshared. Public images and service integration images cannot be shared or unshared.
- If the image ID specified in imageId does not exist, a 404 error is returned.
- Once an image is no longer shared, the user it was unshared from will no longer be able to use it to create or reset instances.
- In the request, the account and accountId parameters are optional but at least one must be provided. If both are included, duplicates will automatically be removed by the server.
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 UnShareImageId := "m-***"
15 // Request to unshare the image with a specified user ID
16 args := &api.SharedUser{
17 // Account ID
18 AccountId: "***",
19 }
20 err := bccClient.UnShareImage(UnShareImageId, args)
21 fmt.Println(err)
22}
Query the list of users with whom the image has been shared
- If the image specified by imageId does not exist, a 404 error will be prompted. You can query the list of users with whom the image has been shared with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5)
6func main() {
7 // Set your AK, SK and the endpoint to be accessed
8 ak := "ak"
9 sk := "sk"
10 endpoint := "http://bcc.bj.baidubce.com"
11 // Create a BCC client
12 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
13 // Image ID
14 imageId := "m-***"
15 result, err := bccClient.GetImageSharedUser(imageId)
16 fmt.Println(result)
17 fmt.Println(err)
18}
Query OS information based on instance IDs in batch
You can query OS information based on instance IDs with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 args := &api.GetImageOsArgs{
15 List of instance IDs
16 InstanceIds: []string{"i-***", "i-***"},
17 }
18 result, err := bccClient.GetImageOS(args)
19 fmt.Println(result)
20 fmt.Println(err)
21}
Bind a tag to an image
You can bind a tag to a specified image with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/model"
5 "github.com/baidubce/bce-sdk-go/services/bcc"
6 "github.com/baidubce/bce-sdk-go/services/bcc/api"
7)
8func main() {
9 // Set your AK, SK and the endpoint to be accessed
10 ak := "ak"
11 sk := "sk"
12 endpoint := "http://bcc.bj.baidubce.com"
13 // Create a BCC client
14 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
15 args := &api.BindTagsRequest{
16 ChangeTags: []model.TagModel{
17 {
18 // Tag key
19 TagKey: "Key***",
20 // Tag value
21 TagValue: "Value***",
22 },
23 {
24 // Tag key
25 TagKey: "Key***",
26 // Tag value
27 TagValue: "Value***",
28 },
29 },
30 }
31 result := bccClient.BindImageToTags("m-***", args)
32 fmt.Println(result)
33}
Unbind a tag from an image
You can bind a tag to a specified image with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/model"
5 "github.com/baidubce/bce-sdk-go/services/bcc"
6 "github.com/baidubce/bce-sdk-go/services/bcc/api"
7)
8func main() {
9 // Set your AK, SK and the endpoint to be accessed
10 ak := "ak"
11 sk := "sk"
12 endpoint := "http://bcc.bj.baidubce.com"
13 // Create a BCC client
14 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
15 args := &api.UnBindTagsRequest{
16 ChangeTags: []model.TagModel{
17 {
18 // Tag key
19 TagKey: "Key***",
20 // Tag value
21 TagValue: "Value***",
22 },
23 },
24 }
25 err := bccClient.UnBindImageToTags(
26 // Image ID
27 "m-***",
28 args)
29 fmt.Println(err)
30}
Import an image
You can import an image with the following code:
Go
1package main
2import (
3 "fmt"
4 "github.com/baidubce/bce-sdk-go/services/bcc"
5 "github.com/baidubce/bce-sdk-go/services/bcc/api"
6)
7func main() {
8 // Set your AK, SK and the endpoint to be accessed
9 ak := "ak"
10 sk := "sk"
11 endpoint := "http://bcc.bj.baidubce.com"
12 // Create a BCC client
13 bccClient, _ := bcc.NewClient(ak, sk, endpoint)
14 args := &api.ImportCustomImageArgs{
15 // OS name
16 OsName: "Centos",
17 // OS bitness
18 OsArch: "64",
19 // OS type
20 OsType: "linux",
21 // OS version
22 OsVersion: "6.5",
23 // Image name, which supports uppercase and lowercase letters, numbers, Chinese characters, and -_/special characters, starting with a letter and a length of 1-65.
24 Name: "image-2",
25 // BOS image address
26 BosURL: "https://bcc-bos.bj.bcebos.com/image_test/CentOS-6-x86_64-GenericCloud.qcow2",
27 }
28 result, _ := bccClient.ImportCustomImage(args)
29 fmt.Println(result)
30}
Query available public image based on instance specifications
You can query available public images based on instance specifications with the following code:
Go
1func TestGetAvailableImagesBySpec(t *testing.T) {
2 args := &api.GetAvailableImagesBySpecArg{
3 OsName: "Centos",
4 Spec: "bcc.ic4.c1m1",
5 MaxKeys: 10,
6 Marker: "m-21bmeYvH",
7 }
8 result, _ := BCC_CLIENT.GetAvailableImagesBySpec(args)
9 fmt.Println(result)
10}
