Image
Create custom image from instance
- Create a custom image with a default limit of 20 images per account. The created images can be used to set up instances.
-
The operation will only succeed if the instance is in a Running or Stopped state.
- Custom images can only be generated 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 client_token = str(uuid.uuid4())
16 image_name = 'instanceImage1' + client_token # Created image name
17 instance_id = 'i-***' # Created instance ID
18 snapshot_id = 'm-***' # Snapshot ID
19 resp = client.create_image_from_snapshot_id(image_name,
20 snapshot_id=snapshot_id,
21 client_token=client_token)
22 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 image_name = 'instanceImage1' + client_token # Created image name
18 instance_id = 'i-***' # Created Instance ID
19 snapshot_id = 'm-***' # Snapshot ID
20 resp = client.create_image_from_snapshot_id(image_name,
21 snapshot_id=snapshot_id,
22 client_token=client_token)
23 print(resp)
Query the image list
The following interface is used to query all image information of the user:
- The returned image information includes public images, custom images, and service integration images.
- Filtering queries by imageType is supported. This parameter is optional. If not specified, it defaults to "All," meaning all types of images will be queried.
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 response = client.list_images()
15 print(response)
Query image details
You can query the detailed information of a single image based on the specified image ID with the following interface:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 imageId = "m-***"
15 response = client.get_image(imageId)
16 print(response)
Delete a custom image
The following interface is used for users to delete specified custom images. Only custom images can be deleted; public images and service integration images cannot be deleted.
- Once an image is deleted, it cannot be recovered and cannot be used to create or reset instances again:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 imageId = "m-***"
15 response = client.delete_image(imageId)
16 print(response)
Cross-region image replication (new)
The following interface is used for cross-region replication of custom images; only custom images can be replicated, while public images and service integration images cannot be replicated:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 imageId = "m-***"
15 destinationImageName = "new-image"
16 destinations = ["hkg"]
17 response = client.remote_copy_image(imageId, destinationImageName, destinations)
18 print(response)
Cancel cross-region image replication (new)
Cancel cross-region replication of custom images:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = b'http://bcc.bj.baidubce.com'
7 AK = b'ak'
8 SK = b'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create client
13 client = bcc_client.BccClient(config)
14 imageId = "m-***"
15 response = client.cancle_remote_copy_image(imageId)
16 print(response)
Share a custom image (new)
The following code is used for users to share their specified custom images. Only custom images can be shared; public images and service integration images cannot be shared:
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6if __name__ == '__main__':
7# Set your AK, SK, and the region to be accessed
8 HOST = b'http://bcc.bj.baidubce.com'
9 AK = b'ak'
10 SK = b'sk'
11# Set default configuration
12 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
13 endpoint=HOST)
14# Create a BCC client
15 client = bcc_client.BccClient(config)
16
17 share_image_id = "m-***"
18 share_account_id = "***"
19# Request to share the image with a specified account
20 response = client.share_image(image_id=share_image_id, # Image ID
21 account_id=share_account_id) # Share Account ID
22 print(response)
Unshare a custom image (new)
Unshare the custom images specified by the user with the following interface:
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6if __name__ == '__main__':
7# Set your AK, SK, and the region to be accessed
8 HOST = b'http://bcc.bj.baidubce.com'
9 AK = b'ak'
10 SK = b'sk'
11# Set default configuration
12 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
13 endpoint=HOST)
14# Create a BCC client
15 client = bcc_client.BccClient(config)
16
17 unshare_image_id = "m-***"
18 unshare_account_id = "***"
19# Request to unshare the image with a specified account
20 response = client.unshare_image(image_id=share_image_id, # Image ID
21 account_id=share_account_id) # Share Account ID
22 print(response)
Query the list of users with whom the image has been shared (new)
You can query the list of users with whom the image has been shared with the following interface:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 image_id = 'm-***' # Image ID
15 print(client.list_shared_user(image_id=image_id).users)
Query OS information (new) by short instance ID in batches
You can query OS information by instance ID in batches with the following interface:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14
15# ID of instance to be queried
16 instance_id1 = 'i-***' # Query Instance ID
17 instance_id2 = 'i-***' # Query Instance ID
18 instance_ids = []
19 instance_ids.append(instance_id1)
20 instance_ids.append(instance_id2)
21 resp = client.list_os(instance_ids=instance_ids)
22 print(resp.os_info)
Bind a tag to an image
You can bind a tag to a specified image with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14
15 tag1 = bcc_model.TagModel(tagKey='key1', # Tag key value
16 tagValue='value1')
17 tag2 = bcc_model.TagModel(tagKey='key2', # Tag key value
18 tagValue='value2')
19 tag_list = []
20 tag_list.append(tag1)
21 tag_list.append(tag2)
22 resp = client.bind_image_to_tags(image_id='m-***', tags=tag_list)
23 print(resp)
Unbind a tag from an image
You can bind a tag to a specified image with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 tag1 = bcc_model.TagModel(tagKey='key1', # Tag key value
15 tagValue='value1')
16 tag2 = bcc_model.TagModel(tagKey='key2', # Tag key value
17 tagValue='value2')
18 tag_list = []
19 tag_list.append(tag1)
20 tag_list.append(tag2)
21 resp = client.unbind_image_to_tags(image_id='m-***', tags=tag_list)
22 print(resp)
Import an image
You can import an image with the following code:
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 resp = client.import_custom_image(
15 os_name='Centos', # Operating system name
16 os_arch='64', # Operating system bitness
17 os_type='linux', # Operating system type
18 os_version='7.2', # Operating system version
19 name='image-01', # Image name, supporting uppercase and lowercase letters, numbers, Chinese characters, and special characters -_ /., starting with a letter and a length of 1-65.
20 bos_url='https://bcc-bos.bj.bcebos.com/image_test/CentOS-6-x86_64-GenericCloud.qcow2') # bos image address
21 print(resp)
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')
6 self.assertEqual(
7 type(resp),
8 baidubce.bce_response.BceResponse)
9 if resp is not None and resp.content is not None:
10 print(json.loads(resp.content.decode('utf-8')))
11 else:
12 print(resp)
