镜像
所有文档
menu

云服务器 BCC

镜像

产品详情自助选购

通过实例创建自定义镜像

使用以下代码用于创建自定义镜像,默认每个账号每个地域配额20个,创建后的镜像可用于创建实例。

只有 Running 或 Stopped 状态的实例才可以执行成功。

仅限通过系统盘快照创建自定义镜像:

import uuid
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client, bcc_model

if __name__ == '__main__':
    # 设置您的ak、sk和要访问的地域
    HOST = 'http://bcc.bj.baidubce.com'
    AK = 'ak'
    SK = 'sk'
    # 设置默认配置
    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
    endpoint=HOST)
    # 创建bcc client
    client = bcc_client.BccClient(config)
    
    client_token = str(uuid.uuid4())
    # 创建的镜像名称
    image_name = 'instanceImage***'
    # 创建的实例id
    instance_id = 'i-***'
    # 快照id
    snapshot_id = 's-B1Cc6Dho'
    resp = client.create_image_from_instance_id(image_name,
                                         instance_id=instance_id,
                                         client_token=client_token)
    print(resp)

通过快照创建自定义镜像

通过快照创建时,只有 Available 状态的快照才可以执行成功:

import uuid

from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client, bcc_model

if __name__ == '__main__':
    # 设置您的ak、sk和要访问的地域
    HOST = 'http://bcc.bj.baidubce.com'
    AK = 'ak'
    SK = 'sk'
    # 设置默认配置
    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
    endpoint=HOST)
    # 创建bcc client
    client = bcc_client.BccClient(config)
    
    client_token = str(uuid.uuid4())
    # 创建的镜像名称
    image_name = 'instanceImage***'
    # 创建的实例id
    instance_id = 'i-***'
    # 快照id
    snapshot_id = 'm-***'

    resp = client.create_image_from_snapshot_id(image_name,
                                         snapshot_id=snapshot_id,
                                         client_token=client_token)
    print(resp)

查询镜像列表

使用如下代码可以查询用户所有的镜像信息:

  • 查询的镜像信息中包括公共镜像、自定义镜像和服务集成镜像。
  • 支持按 imageType 来过滤查询,此参数非必需,未设置时默认为 All,即查询所有类型的镜像:
    def list_images(self):
          #指定要查询何种类型的镜像
          #All(所有)
          #System(系统镜像/公共镜像)
          #Custom(自定义镜像)
          #Integration(服务集成镜像)
          #Sharing(共享镜像)
          #GpuBccSystem(GPU专用公共镜像)
          #GpuBccCustom(GPU专用自定义镜像)
          #FpgaBccSystem(FPGA专用公共镜像)
          #FpgaBccCustom(FPGA专用自定义镜像) 
          image_type = 'All'
          #批量获取列表的查询的起始位置 
          marker = 'your-marker'
          #每页包含的最大数量
          max_keys = 100
          self.assertEqual(
                type(self.client.list_images(image_type=image_type
                                             marker=marker
                                             max_keys=max_keys)), 
                baidubce.bce_response.BceResponse)

查询镜像详情

使用如下代码根据指定镜像ID查询单个镜像的详细信息:

    def get_image(self): 
          #待查询镜像ID
          image_id='your-choose-image-id'
          self.assertEqual(
                type(self.client.get_image(image_id)), 
                baidubce.bce_response.BceResponse)

删除自定义镜像

使用如下代码删除用户自己的指定的自定义镜像,仅限自定义镜像,公共镜像和服务集成镜像不能删除。

镜像删除后无法恢复,不能再用于创建、重置实例:

    def delete_image(self): 
          #待删除镜像ID
          image_id='your-choose-image-id'
          self.assertEqual(
                type(self.client.delete_image(image_id)), 
                baidubce.bce_response.BceResponse)

根据实例规格查询可用公共镜像

使用以下代码可以根据实例规格查询可用公共镜像:

    def test_get_available_images_by_spec(self):
        """
        test case for get_available_images_by_spec
        """
        resp = self.client.get_available_images_by_spec(spec='bcc.ic4.c1m1', os_name='Centos')
上一篇
磁盘
下一篇
快照