镜像
更新时间:2024-07-02
通过实例创建自定义镜像
使用以下代码用于创建自定义镜像,默认每个账号每个地域配额20个,创建后的镜像可用于创建实例。
只有 Running 或 Stopped 状态的实例才可以执行成功。
仅限通过系统盘快照创建自定义镜像:
Python
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
5
6if __name__ == '__main__':
7 # 设置您的ak、sk和要访问的地域
8 HOST = 'http://bcc.bj.baidubce.com'
9 AK = 'ak'
10 SK = 'sk'
11 # 设置默认配置
12 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
13 endpoint=HOST)
14 # 创建bcc client
15 client = bcc_client.BccClient(config)
16
17 client_token = str(uuid.uuid4())
18 # 创建的镜像名称
19 image_name = 'instanceImage***'
20 # 创建的实例id
21 instance_id = 'i-***'
22 # 快照id
23 snapshot_id = 's-B1Cc6Dho'
24 resp = client.create_image_from_instance_id(image_name,
25 instance_id=instance_id,
26 client_token=client_token)
27 print(resp)
通过快照创建自定义镜像
通过快照创建时,只有 Available 状态的快照才可以执行成功:
Python
1import uuid
2
3from baidubce.auth.bce_credentials import BceCredentials
4from baidubce.bce_client_configuration import BceClientConfiguration
5from baidubce.services.bcc import bcc_client, bcc_model
6
7if __name__ == '__main__':
8 # 设置您的ak、sk和要访问的地域
9 HOST = 'http://bcc.bj.baidubce.com'
10 AK = 'ak'
11 SK = 'sk'
12 # 设置默认配置
13 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
14 endpoint=HOST)
15 # 创建bcc client
16 client = bcc_client.BccClient(config)
17
18 client_token = str(uuid.uuid4())
19 # 创建的镜像名称
20 image_name = 'instanceImage***'
21 # 创建的实例id
22 instance_id = 'i-***'
23 # 快照id
24 snapshot_id = 'm-***'
25
26 resp = client.create_image_from_snapshot_id(image_name,
27 snapshot_id=snapshot_id,
28 client_token=client_token)
29 print(resp)
查询镜像列表
使用如下代码可以查询用户所有的镜像信息:
- 查询的镜像信息中包括公共镜像、自定义镜像和服务集成镜像。
- 支持按 imageType 来过滤查询,此参数非必需,未设置时默认为 All,即查询所有类型的镜像:
Python
1 def list_images(self):
2 #指定要查询何种类型的镜像
3 #All(所有)
4 #System(系统镜像/公共镜像)
5 #Custom(自定义镜像)
6 #Integration(服务集成镜像)
7 #Sharing(共享镜像)
8 #GpuBccSystem(GPU专用公共镜像)
9 #GpuBccCustom(GPU专用自定义镜像)
10 #FpgaBccSystem(FPGA专用公共镜像)
11 #FpgaBccCustom(FPGA专用自定义镜像)
12 image_type = 'All'
13 #批量获取列表的查询的起始位置
14 marker = 'your-marker'
15 #每页包含的最大数量
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)
查询镜像详情
使用如下代码根据指定镜像ID查询单个镜像的详细信息:
Python
1 def get_image(self):
2 #待查询镜像ID
3 image_id='your-choose-image-id'
4 self.assertEqual(
5 type(self.client.get_image(image_id)),
6 baidubce.bce_response.BceResponse)
删除自定义镜像
使用如下代码删除用户自己的指定的自定义镜像,仅限自定义镜像,公共镜像和服务集成镜像不能删除。
镜像删除后无法恢复,不能再用于创建、重置实例:
Python
1 def delete_image(self):
2 #待删除镜像ID
3 image_id='your-choose-image-id'
4 self.assertEqual(
5 type(self.client.delete_image(image_id)),
6 baidubce.bce_response.BceResponse)
根据实例规格查询可用公共镜像
使用以下代码可以根据实例规格查询可用公共镜像:
Python
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')