Image
Create custom image from instance
The code below demonstrates how to create a custom image, with a default limit of 20 images per account. The created images can be used to generate instances.
The operation will only succeed if the instance is in a Running or Stopped state.
Custom images can only be created from system disk snapshots:
1import uuid
2from baidubce.auth.bce_credentials import BceCredentials
3from baidubce.bce_client_configuration import BceClientConfiguration
4from baidubce.services.bcc import bcc_client, bcc_model
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = 'http://bcc.bj.baidubce.com'
8 AK = 'ak'
9 SK = 'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16 client_token = str(uuid.uuid4())
17# Name of image created
18 image_name = 'instanceImage***'
19# ID of instance created
20 instance_id = 'i-***'
21# Snapshot ID
22 snapshot_id = 's-B1Cc6Dho'
23 resp = client.create_image_from_instance_id(image_name,
24 instance_id=instance_id,
25 client_token=client_token)
26 print(resp)
Create custom image from snapshot
When creating via a snapshot, the operation can only succeed if the snapshot is in Available status:
1import uuid
2from baidubce.auth.bce_credentials import BceCredentials
3from baidubce.bce_client_configuration import BceClientConfiguration
4from baidubce.services.bcc import bcc_client, bcc_model
5if __name__ == '__main__':
6# Set your AK, SK, and the region to be accessed
7 HOST = 'http://bcc.bj.baidubce.com'
8 AK = 'ak'
9 SK = 'sk'
10# Set default configuration
11 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12 endpoint=HOST)
13# Create a BCC client
14 client = bcc_client.BccClient(config)
15
16 client_token = str(uuid.uuid4())
17# Name of image created
18 image_name = 'instanceImage***'
19# ID of instance created
20 instance_id = 'i-***'
21# Snapshot ID
22 snapshot_id = 'm-***'
23 resp = client.create_image_from_snapshot_id(image_name,
24 snapshot_id=snapshot_id,
25 client_token=client_token)
26 print(resp)
Query the image list
The following code can be used to query all image information of the user:
- The returned image information includes public images, custom images, and service integration images.
- It supports filtering queries by imageType. This parameter is not mandatory. If not set, it defaults to All, which means querying all types of images:
1 def list_images(self):
2 #Specify the type of image to be queried
3 #All (all)
4 #System (system image/public image)
5 #Custom (custom image)
6 #Integration (service integration image)
7 #Sharing (shared image)
8 #GpuBccSystem (GPU-specific public image)
9 #GpuBccSystem (GPU-specific custom image)
10 #FpgaBccSystem (FPGA-specific public image)
11 #FpgaBccCustom (FPGA-specific custom image)
12 image_type = 'All'
13 # Starting position of the batch list query
14 marker = 'your-marker'
15 # Maximum number of items included per page
16 max_keys = 100
17 self.assertEqual(
18 type(self.client.list_images(image_type=image_type
19 marker=marker
20 max_keys=max_keys)),
21 baidubce.bce_response.BceResponse)
Query image details
You can query the detailed information of a single image based on the specified image ID with the following code:
1 def get_image(self):
2 #ID of image to be queried
3 image_id='your-choose-image-id'
4 self.assertEqual(
5 type(self.client.get_image(image_id)),
6 baidubce.bce_response.BceResponse)
Delete a custom image
The code below is for deleting specified custom images. Only custom images can be deleted; public and service integration images cannot be removed.
Once an image is deleted, it cannot be recovered and cannot be used to create or reset instances again:
1 def delete_image(self):
2 #ID of image to be deleted
3 image_id='your-choose-image-id'
4 self.assertEqual(
5 type(self.client.delete_image(image_id)),
6 baidubce.bce_response.BceResponse)
Query available public image based on instance specifications
You can query available public images based on instance specifications with the following code:
1 def test_get_available_images_by_spec(self):
2 """
3 test case for get_available_images_by_spec
4 """
5 resp = self.client.get_available_images_by_spec(spec='bcc.ic4.c1m1', os_name='Centos')
