磁盘
直接创建磁盘
使用以下代码可以创建磁盘:
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 = 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)
# 标签列表
tags = [bcc_model.TagModel("example", "createCds")]
# 自动快照策略
auto_snapshot_policy = bcc_model.AutoSnapshotPolicyModel(name="aspExample", # 自动快照策略名称
timePoints=[1, 2, 3], # 自动快照创建时间点, 参数为0~23的列表
repeatWeekdays=[1, 5], # 自动快照重复日期, 参数为0~6的列表
retentionDays=30) # 自动快照的保留时间,单位为天。 -1:永久保存 1~65536:指定保存天数
resp = client.create_volume_with_cds_size(cds_size_in_gb=5,
charge_type='Postpaid',
zone_name='cn-bj-a', # 可用区名称
instance_id='', # 创建完成后待挂载的实例ID
encrypt_key='c6c0c0b3-****-****-****-ca6d5751504f', # 加密密钥ID
name='test-name', # 磁盘名称
description='desc', # 磁盘描述
renew_time_unit='month', # 续费时长单位,支持:month(月) / year(年)
renew_time=2, # 续费时长,
relation_tag=True, # 关联快照和快照链是否统一加标签
auto_snapshot_policy=auto_snapshot_policy, # 自动快照策略
tags=tags) # 绑定标签
print (resp)
各磁盘类型可配容量范围详见磁盘性能。
从快照创建CDS磁盘
使用以下代码可以从快照创建磁盘:
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 = 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)
resp = client.create_volume_with_snapshot_id(snapshot_id="s-er2Q****", # 快照ID
charge_type='Postpaid', # 支付方式
cluster_id="DC-jh5q****", # 专属集群ID
purchase_count=2) # 购买数量
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)
resp = client.list_volumes(instance_id="i-cVh8****", # 磁盘挂载的实例ID
zone_name="cn-bj-c", # 可用区
marker="v-3uIJ****", # 起始卷ID
max_keys=10, # 返回列表的最大数量
cluster_id="DC-rWKx****") # CDS集群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)
resp = client.get_volume(volume_id='v-3uIJ****') # 指定磁盘ID
print (resp)
挂载CDS磁盘
使用以下代码将磁盘挂载在指定的BCC实例下,注意CDS磁盘需要挂载在与其处在相同可用区下的BCC实例上。
只有磁盘状态为 Available 且实例状态为 Running 或 Stopped 时才允许挂载:
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
volume_id = 'v-***'
# 设置要挂载的实例ID
instance_id = 'i-***'
resp = client.attach_volume(volume_id, instance_id)
print(resp)
卸载CDS磁盘
使用以下代码将指定的磁盘从实例中卸载。
只有实例状态为 Running 或 Stopped 时,磁盘才可以执行此操作:
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
volume_id = 'v-***'
# 设置所挂载的实例ID
instance_id = 'i-***'
resp = client.detach_volume(volume_id, instance_id)
print(resp)
释放CDS磁盘
使用以下代码释放未挂载的CDS磁盘,系统盘不能释放:
- 磁盘释放后不可恢复。默认情况下,该磁盘的所有快照将保留,但会删除与磁盘的关联关系。
-
只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作。
def release_volume(self):
#设置要释放的磁盘ID volume_id = 'your-choose-volume-id' self.assertEqual( type(self.client.release_volume(volume_id)), baidubce.bce_response.BceResponse)
磁盘扩容
使用以下代码对磁盘进行扩容:
- 磁盘只能进行扩容操作,不支持缩容。
- 只有Available状态的磁盘,才能进行扩容操作。
- 只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作。
- 各磁盘类型可配容量范围详见磁盘性能。
# !/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)
volume_id = "v-***"
# 请求扩容磁盘,变更磁盘类型和大小
response = client.resize_volume(volume_id=volume_id, # 磁盘ID
new_cds_size=50, # 扩容后磁盘大小, 单位为GB
new_volume_type="cloud_hp1") # 扩容后磁盘类型, 可取值见:https://cloud.baidu.com/doc/BCC/s/6jwvyo0q2#storagetype
print(response)
回滚磁盘
使用以下代码用指定磁盘自身的快照回滚磁盘内容:
- 磁盘状态必须为 Available 才可以执行此操作。
- 指定 snapshotId 必须是由 volumeId 创建的快照。
- 若是回滚系统盘,实例状态必须为 Running 或 Stopped 才可以执行此操作。
-
回滚系统盘快照,自本次快照以来的系统盘数据将全部丢失,不可恢复:
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 = 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) volume_id = 'v-vG0z**' # 用于回滚指定磁盘的快照ID,必须为 volumeId 所创建的快照 snapshot_id = 's-HkbJ**' resp = client.rollback_volume(volume_id, snapshot_id) 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 = 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-PGE6****'
# 设置续费时长,目前只支持续费单位为月
billing = bcc_model.Billing('Prepaid', 2)
resp = client.purchase_reserved_volume(volume_id=volume_id, billing=billing)
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 = 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-vG0z****'
# 设置磁盘新名称
cds_name = 'new-cds-name'
resp = client.modify_volume_Attribute(volume_id=volume_id, cds_name=cds_name)
print (resp)
修改磁盘计费方式(新)
使用以下代码可以将按量付费(后付费)磁盘变更为包年包月(预付费): # !/usr/bin/env python # coding=utf-8 from baidubce.services.bcc import bcc_client from baidubce.services.bcc import bcc_model 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)
volume_id = "v-***"
post_paid_billing = bcc_model.Billing(paymentTiming='Prepaid', # 预付费计费方式, 指定paymentTiming为Prepaid, 可不填
reservationLength=3, # 预付费时长, 单位为月时取值范围为1-9
reservationTimeUnit='Month') # 预付费时长单位,当前支持取值为Month
# 请求将后付费磁盘变更计费方式为预付费
response = client.modify_volume_charge_type(volume_id=volume_id, # 磁盘ID
billing=post_paid_billing) # 计费方式
print(response)
使用以下代码可以将包年包月(预付费)磁盘变更为按量付费(后付费): # !/usr/bin/env python # coding=utf-8 from baidubce.services.bcc import bcc_client from baidubce.services.bcc import bcc_model 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)
# 预付费磁盘变更计费方式为后付费
volume_id = "v-***"
post_paid_billing = bcc_model.Billing(paymentTiming='Postpaid', # 后付费计费方式, 指定paymentTiming为Postpaid, 可不填
reservationLength=0) # 后付费情况下,必须需要设置reservationLength为0
# 请求将预付费磁盘变更计费方式为后付费
response = client.modify_volume_charge_type(volume_id=volume_id, # 磁盘ID
billing=post_paid_billing) # 计费方式
print(response)
释放cds磁盘(新)
使用以下代码可以实现释放CDS磁盘
- 该接口用于释放未挂载的CDS磁盘,系统盘不能释放
- 只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作,否则将提示 409 错误
- 如果 volumeId 的磁盘不存在,将提示 404 错误
- 可以控制是否释放关联的自动/手动快照、是否进入回收站
# !/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)
volume_id = "v-***"
# 请求释放cds盘
response = client.release_volume_new(volume_id=volume_id, # 磁盘ID
auto_snapshot="on", # 是否删除磁盘关联的自动快照
manual_snapshot="on", # 是否删除磁盘关联的手动快照
recycle="off") # 是否将磁盘回收至回收站,为off时直接释放,不进入回收站
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)
volume_id = "v-***"
# 请求开通自动续费
response = client.auto_renew_cds_volume(volume_id=volume_id, # 磁盘ID
renew_time=3, # 续费时长, 单位为月时取值范围为1-9
renew_time_unit="month") # 续费时长单位,当前支持取值为: month
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)
volume_id = "v-***"
# 请求取消自动续费
response = client.cancel_auto_renew_cds_volume(volume_id=volume_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)
azone_name = "cn-bj-c" # 可用区名称;当传入azone_name为空串或为非法azone时,会返回全部可用区的可购买磁盘信息。
# 请求查询对应可用区下可购买磁盘信息
response = client.get_available_disk_info(zone_name=azone_name)
print(response)
磁盘绑定标签
使用以下代码可以给指定磁盘绑定标签:
# !/usr/bin/env python
# coding=utf-8
from baidubce.services.bcc import bcc_client
from baidubce.services.bcc import bcc_model
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)
volume_id = "v-***" # 磁盘ID
tag1 = bcc_model.TagModel(tagKey='TagKey01', # Tag key
tagValue='TagValue01') # Tag value
tag2 = bcc_model.TagModel(tagKey='TagKey02', # Tag key
tagValue='TagValue02') # Tag value
tag_list = list() # 标签列表
tag_list.append(tag1)
tag_list.append(tag2)
# 请求为磁盘绑定标签
response = client.tag_volume(volume_id=volume_id, # 磁盘ID
relation_tag=False, # 是否为相关资源绑定标签
tags=tag_list) # 标签列表
print(response)
磁盘解绑标签
使用以下代码可以给指定磁盘解绑标签:
# !/usr/bin/env python
# coding=utf-8
from baidubce.services.bcc import bcc_client
from baidubce.services.bcc import bcc_model
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)
volume_id = "v-***" # 磁盘ID
tag1 = bcc_model.TagModel(tagKey='TagKey01', # Tag key
tagValue='TagValue01') # Tag value
tag2 = bcc_model.TagModel(tagKey='TagKey02', # Tag key
tagValue='TagValue02') # Tag value
tag_list = list() # 标签列表
tag_list.append(tag1)
tag_list.append(tag2)
# 请求为磁盘解绑标签
response = client.untag_volume(volume_id=volume_id, # 磁盘ID
relation_tag=False, # 是否为相关资源绑定标签
tags=tag_list) # 标签列表
print(response)