快照
更新时间:2024-07-02
创建快照
用于从指定的磁盘创建快照,支持创建系统盘快照和CDS磁盘快照。
创建系统盘快照时,实例的状态必须为 Running 或 Stopped 此操作才能正常完成。
创建CDS磁盘快照时,磁盘的状态需要为InUse或 Available 此操作才能正常完成。
可创建快照数量的配额取决于账号下拥有的磁盘总数,当前最多为磁盘数量乘以8:
import uuid
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)
client_token = str(uuid.uuid4())
# 设置快照名称
snapshot_name = 'snapshot***'
# 创建快照的磁盘ID
volume_id = 'v-***'
resp = client.create_snapshot(volume_id, snapshot_name, client_token=client_token)
print(resp)
查询快照列表
使用如下代码查询所有的快照信息,包括系统盘快照和数据盘快照信息:
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)
# 创建快照的磁盘ID
volume_id = 'v-***'
# 批量获取列表的查询的起始位置
marker = "your-marker"
# 每页包含的最大数量
max_keys = 100
resp = client.list_snapshots(marker=marker, max_keys=max_keys, volume_id=volume_id)
print(resp)
查询快照详情
使用如下代码可以通过用户的指定的快照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)
# 快照ID
snapshot_id = 's-***'
resp = client.get_snapshot(snapshot_id)
print(resp)
删除快照
使用如下的代码可以通过指定快照ID删除快照:
只有当快照的状态为 CreatedFailed 或 Available时,操作才能正常完成:
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)
# 待删除快照ID
snapshot_id = 's-***'
resp = client.delete_snapshot(snapshot_id=snapshot_id)
print(resp)