磁盘
所有文档
menu

云服务器 BCC

磁盘

直接创建磁盘

使用以下代码可以创建磁盘:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = b'http://bcc.bj.baidubce.com'
8    AK = b'ak'
9    SK = b'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15
16
17    # 标签列表
18    tags = [bcc_model.TagModel("example", "createCds")]
19
20    # 自动快照策略
21    auto_snapshot_policy = bcc_model.AutoSnapshotPolicyModel(name="aspExample",     # 自动快照策略名称
22                                                             timePoints=[1, 2, 3],  # 自动快照创建时间点, 参数为0~23的列表
23                                                             repeatWeekdays=[1, 5], # 自动快照重复日期, 参数为0~6的列表
24                                                             retentionDays=30)     # 自动快照的保留时间,单位为天。 -1:永久保存 1~65536:指定保存天数
25
26    resp = client.create_volume_with_cds_size(cds_size_in_gb=5,
27                                              charge_type='Postpaid',
28                                              zone_name='cn-bj-a', # 可用区名称
29                                              instance_id='', # 创建完成后待挂载的实例ID
30                                              encrypt_key='c6c0c0b3-****-****-****-ca6d5751504f', # 加密密钥ID
31                                              name='test-name', # 磁盘名称
32                                              description='desc', # 磁盘描述
33                                              renew_time_unit='month', # 续费时长单位,支持:month(月) / year(年)
34                                              renew_time=2, # 续费时长,
35                                              relation_tag=True,  # 关联快照和快照链是否统一加标签
36                                              auto_snapshot_policy=auto_snapshot_policy, # 自动快照策略
37                                              tags=tags) # 绑定标签
38    print (resp)

各磁盘类型可配容量范围详见磁盘性能

从快照创建CDS磁盘

使用以下代码可以从快照创建磁盘:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = b'http://bcc.bj.baidubce.com'
8    AK = b'ak'
9    SK = b'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15
16
17    resp = client.create_volume_with_snapshot_id(snapshot_id="s-er2Q****",  # 快照ID
18                                                 charge_type='Postpaid',  # 支付方式
19                                                 cluster_id="DC-jh5q****",  # 专属集群ID
20                                                 purchase_count=2)  # 购买数量
21    print (resp)

查询磁盘列表

使用以下代码查询所有的磁盘列表:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = b'http://bcc.bj.baidubce.com'
8    AK = b'ak'
9    SK = b'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15
16    resp = client.list_volumes(instance_id="i-cVh8****",  # 磁盘挂载的实例ID
17                               zone_name="cn-bj-c",  # 可用区
18                               marker="v-3uIJ****",  # 起始卷ID
19                               max_keys=10, # 返回列表的最大数量
20                               cluster_id="DC-rWKx****") # CDS集群id
21    print (resp)

查询磁盘详情

使用以下代码可以根据磁盘ID查询单个磁盘的详细信息:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = b'http://bcc.bj.baidubce.com'
8    AK = b'ak'
9    SK = b'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15
16    resp = client.get_volume(volume_id='v-3uIJ****') # 指定磁盘ID
17    print (resp)
18    

挂载CDS磁盘

使用以下代码将磁盘挂载在指定的BCC实例下,注意CDS磁盘需要挂载在与其处在相同可用区下的BCC实例上。

只有磁盘状态为 Available 且实例状态为 Running 或 Stopped 时才允许挂载:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = 'http://bcc.bj.baidubce.com'
8    AK = 'ak'
9    SK = 'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15    # 设置要挂载的磁盘ID
16    volume_id = 'v-***'
17    # 设置要挂载的实例ID
18    instance_id = 'i-***'
19    resp = client.attach_volume(volume_id, instance_id)
20    print(resp)

卸载CDS磁盘

使用以下代码将指定的磁盘从实例中卸载。

只有实例状态为 Running 或 Stopped 时,磁盘才可以执行此操作:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = 'http://bcc.bj.baidubce.com'
8    AK = 'ak'
9    SK = 'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15    
16    # 设置要卸载的磁盘ID
17    volume_id = 'v-***'
18    # 设置所挂载的实例ID
19    instance_id = 'i-***'
20    resp = client.detach_volume(volume_id, instance_id)
21    print(resp)

释放CDS磁盘

使用以下代码释放未挂载的CDS磁盘,系统盘不能释放:

  • 磁盘释放后不可恢复。默认情况下,该磁盘的所有快照将保留,但会删除与磁盘的关联关系。
  • 只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作。

    def release_volume(self):

    Plain Text
    1    #设置要释放的磁盘ID
    2    volume_id = 'your-choose-volume-id'
    3
    4    self.assertEqual(
    5
    6          type(self.client.release_volume(volume_id)), 
    7
    8          baidubce.bce_response.BceResponse)

磁盘扩容

使用以下代码对磁盘进行扩容:

  • 磁盘只能进行扩容操作,不支持缩容。
  • 只有Available状态的磁盘,才能进行扩容操作。
  • 只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作。
  • 各磁盘类型可配容量范围详见磁盘性能
Python
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6
7
8if __name__ == '__main__':
9    # 设置您的ak、sk和要访问的地域
10    HOST = b'http://bcc.bj.baidubce.com'
11    AK = b'ak'
12    SK = b'sk'
13    # 设置默认配置
14    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
15                                    endpoint=HOST)
16    # 创建bcc client
17    client = bcc_client.BccClient(config)
18    
19    volume_id = "v-***"
20    
21    # 请求扩容磁盘,变更磁盘类型和大小
22    response = client.resize_volume(volume_id=volume_id,             # 磁盘ID
23                                    new_cds_size=50,                 # 扩容后磁盘大小, 单位为GB
24                                    new_volume_type="cloud_hp1")     # 扩容后磁盘类型, 可取值见:https://cloud.baidu.com/doc/BCC/s/6jwvyo0q2#storagetype
25    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)

磁盘续费

使用以下代码可以实现磁盘续费。

磁盘扩缩容期间不能进行续费操作:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = b'http://bcc.bj.baidubce.com'
8    AK = b'ak'
9    SK = b'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15    # 待续费磁盘ID
16    volume_id = 'v-PGE6****'
17    # 设置续费时长,目前只支持续费单位为月
18    billing = bcc_model.Billing('Prepaid', 2)
19    resp = client.purchase_reserved_volume(volume_id=volume_id, billing=billing)
20    print (resp)

修改磁盘属性(新)

使用以下代码可以修改磁盘属性:

Plain Text
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client, bcc_model
4
5if __name__ == '__main__':
6    # 设置您的ak、sk和要访问的地域
7    HOST = b'http://bcc.bj.baidubce.com'
8    AK = b'ak'
9    SK = b'sk'
10    # 设置默认配置
11    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                    endpoint=HOST)
13    # 创建bcc client
14    client = bcc_client.BccClient(config)
15    # 待修改磁盘ID
16    volume_id = 'v-vG0z****'
17    # 设置磁盘新名称
18    cds_name = 'new-cds-name'
19    resp = client.modify_volume_Attribute(volume_id=volume_id, cds_name=cds_name)
20    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

Plain Text
1if __name__ == '__main__':
2    # 设置您的ak、sk和要访问的地域
3    HOST = b'http://bcc.bj.baidubce.com'
4    AK = b'ak'
5    SK = b'sk'
6    # 设置默认配置
7    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
8                                    endpoint=HOST)
9    # 创建bcc client
10    client = bcc_client.BccClient(config)
11    
12    volume_id = "v-***"
13    post_paid_billing = bcc_model.Billing(paymentTiming='Prepaid',          # 预付费计费方式, 指定paymentTiming为Prepaid, 可不填
14                                        reservationLength=3,              # 预付费时长, 单位为月时取值范围为1-9
15                                        reservationTimeUnit='Month')       # 预付费时长单位,当前支持取值为Month
16
17    # 请求将后付费磁盘变更计费方式为预付费
18    response = client.modify_volume_charge_type(volume_id=volume_id,            # 磁盘ID
19                                                billing=post_paid_billing)      # 计费方式
20    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

Plain Text
1if __name__ == '__main__':
2    # 设置您的ak、sk和要访问的地域
3    HOST = b'http://bcc.bj.baidubce.com'
4    AK = b'ak'
5    SK = b'sk'
6    # 设置默认配置
7    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
8                                    endpoint=HOST)
9    # 创建bcc client
10    client = bcc_client.BccClient(config)
11    
12    # 预付费磁盘变更计费方式为后付费
13    volume_id = "v-***"
14    post_paid_billing = bcc_model.Billing(paymentTiming='Postpaid',       # 后付费计费方式, 指定paymentTiming为Postpaid, 可不填
15                                        reservationLength=0)              # 后付费情况下,必须需要设置reservationLength为0
16
17    # 请求将预付费磁盘变更计费方式为后付费
18    response = client.modify_volume_charge_type(volume_id=volume_id,        # 磁盘ID
19                                                billing=post_paid_billing)  # 计费方式
20    print(response)

释放cds磁盘(新)

使用以下代码可以实现释放CDS磁盘

  • 该接口用于释放未挂载的CDS磁盘,系统盘不能释放
  • 只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作,否则将提示 409 错误
  • 如果 volumeId 的磁盘不存在,将提示 404 错误
  • 可以控制是否释放关联的自动/手动快照、是否进入回收站
Python
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6
7
8if __name__ == '__main__':
9    # 设置您的ak、sk和要访问的地域
10    HOST = b'http://bcc.bj.baidubce.com'
11    AK = b'ak'
12    SK = b'sk'
13    # 设置默认配置
14    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
15                                    endpoint=HOST)
16    # 创建bcc client
17    client = bcc_client.BccClient(config)
18    
19    volume_id = "v-***"
20    
21    # 请求释放cds盘
22    response = client.release_volume_new(volume_id=volume_id,                      # 磁盘ID
23                                         auto_snapshot="on",                       # 是否删除磁盘关联的自动快照
24                                         manual_snapshot="on",                     # 是否删除磁盘关联的手动快照
25                                         recycle="off")                            # 是否将磁盘回收至回收站,为off时直接释放,不进入回收站
26    print(response)

磁盘开通自动续费

使用以下代码可以开通磁盘自动续费:

Python
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6
7
8if __name__ == '__main__':
9    # 设置您的ak、sk和要访问的地域
10    HOST = b'http://bcc.bj.baidubce.com'
11    AK = b'ak'
12    SK = b'sk'
13    # 设置默认配置
14    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
15                                    endpoint=HOST)
16    # 创建bcc client
17    client = bcc_client.BccClient(config)
18    
19    volume_id = "v-***"
20    # 请求开通自动续费
21    response = client.auto_renew_cds_volume(volume_id=volume_id,         # 磁盘ID
22                                            renew_time=3,                # 续费时长, 单位为月时取值范围为1-9
23                                            renew_time_unit="month")     # 续费时长单位,当前支持取值为: month
24    print(response)

磁盘取消自动续费

使用以下代码可以取消磁盘自动续费:

Python
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6
7
8if __name__ == '__main__':
9    # 设置您的ak、sk和要访问的地域
10    HOST = b'http://bcc.bj.baidubce.com'
11    AK = b'ak'
12    SK = b'sk'
13    # 设置默认配置
14    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
15                                    endpoint=HOST)
16    # 创建bcc client
17    client = bcc_client.BccClient(config)
18    
19    volume_id = "v-***"
20    # 请求取消自动续费
21    response = client.cancel_auto_renew_cds_volume(volume_id=volume_id)     # 磁盘ID
22    print(response)

查询可用区的磁盘信息

使用以下代码可以查询可用区的磁盘信息:

Python
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.auth.bce_credentials import BceCredentials
5from baidubce.bce_client_configuration import BceClientConfiguration
6
7
8if __name__ == '__main__':
9    # 设置您的ak、sk和要访问的地域
10    HOST = b'http://bcc.bj.baidubce.com'
11    AK = b'ak'
12    SK = b'sk'
13    # 设置默认配置
14    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
15                                    endpoint=HOST)
16    # 创建bcc client
17    client = bcc_client.BccClient(config)
18    
19    azone_name = "cn-bj-c"                  # 可用区名称;当传入azone_name为空串或为非法azone时,会返回全部可用区的可购买磁盘信息。
20    
21    # 请求查询对应可用区下可购买磁盘信息
22    response = client.get_available_disk_info(zone_name=azone_name)
23    print(response)

磁盘绑定标签

使用以下代码可以给指定磁盘绑定标签:

Python
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.services.bcc import bcc_model
5from baidubce.auth.bce_credentials import BceCredentials
6from baidubce.bce_client_configuration import BceClientConfiguration
7
8
9if __name__ == '__main__':
10    # 设置您的ak、sk和要访问的地域
11    HOST = b'http://bcc.bj.baidubce.com'
12    AK = b'ak'
13    SK = b'sk'
14    # 设置默认配置
15    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
16                                    endpoint=HOST)
17    # 创建bcc client
18    client = bcc_client.BccClient(config)
19    
20    volume_id = "v-***"                                       # 磁盘ID
21    tag1 = bcc_model.TagModel(tagKey='TagKey01',               # Tag key
22                              tagValue='TagValue01')           # Tag value
23    tag2 = bcc_model.TagModel(tagKey='TagKey02',               # Tag key
24                              tagValue='TagValue02')           # Tag value
25    tag_list = list()                                          # 标签列表
26    tag_list.append(tag1)
27    tag_list.append(tag2)
28    
29    # 请求为磁盘绑定标签
30    response = client.tag_volume(volume_id=volume_id,       # 磁盘ID
31                                 relation_tag=False,        # 是否为相关资源绑定标签
32                                 tags=tag_list)             # 标签列表
33    print(response)

磁盘解绑标签

使用以下代码可以给指定磁盘解绑标签:

Python
1# !/usr/bin/env python
2# coding=utf-8
3from baidubce.services.bcc import bcc_client
4from baidubce.services.bcc import bcc_model
5from baidubce.auth.bce_credentials import BceCredentials
6from baidubce.bce_client_configuration import BceClientConfiguration
7
8
9if __name__ == '__main__':
10    # 设置您的ak、sk和要访问的地域
11    HOST = b'http://bcc.bj.baidubce.com'
12    AK = b'ak'
13    SK = b'sk'
14    # 设置默认配置
15    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
16                                    endpoint=HOST)
17    # 创建bcc client
18    client = bcc_client.BccClient(config)
19    
20    volume_id = "v-***"                                       # 磁盘ID
21    tag1 = bcc_model.TagModel(tagKey='TagKey01',               # Tag key
22                              tagValue='TagValue01')           # Tag value
23    tag2 = bcc_model.TagModel(tagKey='TagKey02',               # Tag key
24                              tagValue='TagValue02')           # Tag value
25    tag_list = list()                                          # 标签列表
26    tag_list.append(tag1)
27    tag_list.append(tag2)
28    
29    # 请求为磁盘解绑标签
30    response = client.untag_volume(volume_id=volume_id,       # 磁盘ID
31                                   relation_tag=False,        # 是否为相关资源绑定标签
32                                   tags=tag_list)             # 标签列表
33    print(response)
上一篇
实例
下一篇
镜像