磁盘
所有文档
menu

云服务器 BCC

磁盘

直接创建磁盘

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

Python
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    tags = [bcc_model.TagModel("example", "createCds")]
18
19    # 自动快照策略
20    auto_snapshot_policy = bcc_model.AutoSnapshotPolicyModel(name="aspExample",     # 自动快照策略名称
21                                                             timePoints=[1, 2, 3],  # 自动快照创建时间点, 参数为0~23的列表
22                                                             repeatWeekdays=[1, 5], # 自动快照重复日期, 参数为0~6的列表
23                                                             retentionDays=30)     # 自动快照的保留时间,单位为天。 -1:永久保存 1~65536:指定保存天数
24
25    resp = client.create_volume_with_cds_size(cds_size_in_gb=5,
26                                     
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磁盘

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

Python
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和要访问的地域
7HOST = b'http://bcc.bj.baidubce.com'
8AK = b'ak'
9SK = b'sk'
10# 设置默认配置
11config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                endpoint=HOST)
13# 创建bcc client
14client = bcc_client.BccClient(config)
15
16
17resp = 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)  # 购买数量
21print (resp)

查询磁盘列表

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

Python
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和要访问的地域
7HOST = b'http://bcc.bj.baidubce.com'
8AK = b'ak'
9SK = b'sk'
10# 设置默认配置
11config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                endpoint=HOST)
13# 创建bcc client
14client = bcc_client.BccClient(config)
15
16resp = 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
21print (resp)

查询磁盘详情

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

Python
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)

挂载CDS磁盘

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

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

Python
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 时,磁盘才可以执行此操作。

Python
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 时才可以执行此操作:

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

释放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
7if __name__ == '__main__':
8    # 设置您的ak、sk和要访问的地域
9    HOST = b'http://bcc.bj.baidubce.com'
10    AK = b'ak'
11    SK = b'sk'
12    # 设置默认配置
13    config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
14                                    endpoint=HOST)
15    # 创建bcc client
16    client = bcc_client.BccClient(config)
17    
18    volume_id = "v-***"
19    
20    # 请求释放cds盘
21    response = client.release_volume_new(
22        # 磁盘ID
23        volume_id=volume_id,
24        # 是否删除磁盘关联的自动快照
25        # auto_snapshot="on",
26        # 是否删除磁盘关联的手动快照
27        # manual_snapshot="on",
28        # 是否将磁盘回收至回收站,为off时直接释放,不进入回收站
29        # recycle="off")
30    print(response)

磁盘扩容

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

  • 磁盘只能进行扩容操作,不支持缩容。
  • 只有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    response = client.resize_volume(
22        # 磁盘ID
23        volume_id=volume_id,
24        # 扩容后磁盘大小, 单位为GB
25        # new_cds_size=50,
26        # 扩容后磁盘类型, 可取值见:https://cloud.baidu.com/doc/BCC/s/6jwvyo0q2#storagetype
27        # new_volume_type="cloud_hp1")
28    print(response)

回滚磁盘

使用以下代码用指定磁盘自身的快照回滚磁盘内容:

  • 磁盘状态必须为 Available 才可以执行此操作。
  • 指定 snapshotId 必须是由 volumeId 创建的快照。
  • 若是回滚系统盘,实例状态必须为 Running 或 Stopped 才可以执行此操作。
  • 回滚系统盘快照,自本次快照以来的系统盘数据将全部丢失,不可恢复。
Python
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和要访问的地域
7HOST = b'http://bcc.bj.baidubce.com'
8AK = b'ak'
9SK = b'sk'
10# 设置默认配置
11config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
12                                endpoint=HOST)
13# 创建bcc client
14client = bcc_client.BccClient(config)
15volume_id = 'v-vG0z****'
16# 用于回滚指定磁盘的快照ID,必须为 volumeId 所创建的快照
17snapshot_id = 's-HkbJ****'
18resp = client.rollback_volume(volume_id, snapshot_id)
19print (resp)

磁盘续费

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

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

Python
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)
上一篇
实例
下一篇
镜像