镜像
更新时间:2024-07-02
通过实例创建自定义镜像
- 用于创建自定义镜像,默认每个账号配额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 = 'instanceImage1' + client_token # 创建的镜像名称
instance_id = 'i-***' # 创建的实例id
snapshot_id = 'm-***' # 快照id
resp = client.create_image_from_snapshot_id(image_name,
snapshot_id=snapshot_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 = 'instanceImage1' + client_token # 创建的镜像名称
instance_id = 'i-***' # 创建的实例id
snapshot_id = 'm-***' # 快照id
resp = client.create_image_from_snapshot_id(image_name,
snapshot_id=snapshot_id,
client_token=client_token)
print(resp)
查询镜像列表
使用以下接口查询用户所有的镜像信息:
- 查询的镜像信息中包括公共镜像、自定义镜像和服务集成镜像。
- 支持按 imageType 来过滤查询,此参数非必需,未设置时默认为 All,即查询所有类型的镜像。
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
response = client.list_images()
print(response)
查询镜像详情
使用以下接口根据指定镜像ID查询单个镜像的详细信息:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
imageId = "m-***"
response = client.get_image(imageId)
print(response)
删除自定义镜像
使用以下接口删除用户自己的指定的自定义镜像,仅限自定义镜像,公共镜像和服务集成镜像不能删除
- 镜像删除后无法恢复,不能再用于创建、重置实例:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
imageId = "m-***"
response = client.delete_image(imageId)
print(response)
跨地域复制镜像(新)
使用以下接口跨地域复制自定义镜像,仅限自定义镜像,公共镜像和服务集成镜像不能复制:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
imageId = "m-***"
destinationImageName = "new-image"
destinations = ["hkg"]
response = client.remote_copy_image(imageId, destinationImageName, destinations)
print(response)
取消跨地域复制镜像(新)
用于取消跨地域复制自定义镜像:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建 client
client = bcc_client.BccClient(config)
imageId = "m-***"
response = client.cancle_remote_copy_image(imageId)
print(response)
共享自定义镜像(新)
使用以下接口可以共享用户自己的指定的自定义镜像,仅限自定义镜像,公共镜像和服务集成镜像不能共享:
# !/usr/bin/env python
# coding=utf-8
from baidubce.services.bcc import bcc_client
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
share_image_id = "m-***"
share_account_id = "***"
# 请求将镜像共享给指定账号
response = client.share_image(image_id=share_image_id, # 镜像ID
account_id=share_account_id) # 共享账号ID
print(response)
取消共享自定义镜像(新)
使用以下接口可以取消共享用户指定的自定义镜像:
# !/usr/bin/env python
# coding=utf-8
from baidubce.services.bcc import bcc_client
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = b'http://bcc.bj.baidubce.com'
AK = b'ak'
SK = b'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
unshare_image_id = "m-***"
unshare_account_id = "***"
# 请求取消将镜像共享给指定账号
response = client.unshare_image(image_id=share_image_id, # 镜像ID
account_id=share_account_id) # 共享账号ID
print(response)
查询已共享用户列表(新)
使用以下接口查询镜像已共享的用户列表:
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)
image_id = 'm-***' # 镜像id
print(client.list_shared_user(image_id=image_id).users)
根据实例短id批量查询os信息(新)
使用以下接口可以根据实例id批量查询OS信息:
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)
# 待查询实例id
instance_id1 = 'i-***' # 查询实例Id
instance_id2 = 'i-***' # 查询实例Id
instance_ids = []
instance_ids.append(instance_id1)
instance_ids.append(instance_id2)
resp = client.list_os(instance_ids=instance_ids)
print(resp.os_info)
镜像绑定标签
使用以下代码可以给指定镜像绑定标签:
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)
tag1 = bcc_model.TagModel(tagKey='key1', # 标签key value
tagValue='value1')
tag2 = bcc_model.TagModel(tagKey='key2', # 标签key value
tagValue='value2')
tag_list = []
tag_list.append(tag1)
tag_list.append(tag2)
resp = client.bind_image_to_tags(image_id='m-***', tags=tag_list)
print(resp)
镜像解绑标签
使用以下代码可以给指定镜像绑定标签:
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)
tag1 = bcc_model.TagModel(tagKey='key1', # 标签key value
tagValue='value1')
tag2 = bcc_model.TagModel(tagKey='key2', # 标签key value
tagValue='value2')
tag_list = []
tag_list.append(tag1)
tag_list.append(tag2)
resp = client.unbind_image_to_tags(image_id='m-***', tags=tag_list)
print(resp)
导入镜像
使用以下代码可以导入一个镜像:
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)
resp = client.import_custom_image(
os_name='Centos', # 操作系统名称
os_arch='64', # 操作系统位数
os_type='linux', # 操作系统类型
os_version='7.2', # 操作系统版本
name='image-01', # 镜像名称,支持大小写字母、数字、中文以及-_ /.特殊字符,必须以字母开头,长度1-65。
bos_url='https://bcc-bos.bj.bcebos.com/image_test/CentOS-6-x86_64-GenericCloud.qcow2') # bos镜像地址
print(resp)
根据实例规格查询可用公共镜像
使用以下代码可以根据实例规格查询可用公共镜像:
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')
self.assertEqual(
type(resp),
baidubce.bce_response.BceResponse)
if resp is not None and resp.content is not None:
print(json.loads(resp.content.decode('utf-8')))
else:
print(resp)