磁盘
更新时间:2024-07-02
创建CDS磁盘
支持新建空白CDS磁盘或者从CDS数据盘快照创建CDS磁盘,参考以下代码可以创建CDS磁盘:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.Billing;
import com.baidubce.services.bcc.model.StorageType;
import com.baidubce.services.bcc.model.volume.CreateVolumeRequest;
import com.baidubce.services.bcc.model.volume.CreateVolumeResponse;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
CreateVolumeRequest request = new CreateVolumeRequest()
.withChargeType("Postpaid") // 计费方式
.withCdsSizeInGB(5) // 磁盘大小
.withStorageType(StorageType.hdd.name()) // 存储类型
.withPurchaseCount(2) // 购买数量
.withZoneName("cn-bj-c"); // 可用区
CreateVolumeResponse response = client.createVolume(request);
System.out.println(response.getVolumeIds());
}
}
从快照创建CDS磁盘
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.Billing;
import com.baidubce.services.bcc.model.Reservation;
import com.baidubce.services.bcc.model.StorageType;
import com.baidubce.services.bcc.model.volume.CreateVolumeRequest;
import com.baidubce.services.bcc.model.volume.CreateVolumeResponse;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
CreateVolumeRequest request = new CreateVolumeRequest()
.withChargeType("Postpaid") // 计费方式
.withReservation(new Reservation().withReservationLength(2).withReservationTimeUnit("month")))
.withSnapshotId("s-4CMA****") //快照ID
.withPurchaseCount(1) // 创建数量
.withZoneName("cn-bj-a"); // 可用区;
CreateVolumeResponse response = client.createVolume(request);
System.out.println(response.getVolumeIds());
}
}
查询磁盘列表
以下代码可以查询所有的磁盘列表,不包含临时数据盘,支持分页查询以及通过云磁盘所挂载的BCC实例ID进行过滤筛选:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.ListVolumesRequest;
import com.baidubce.services.bcc.model.volume.ListVolumesResponse;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
ListVolumesRequest listVolumesRequest = new ListVolumesRequest()
.withZoneName("cn-bj-c") // 可用区
.withInstanceId("i-lC39****") // 实例ID
.withMarker("v-ePrp****") // 起始卷ID
.withMaxKeys(10); // 最大返回数量
ListVolumesResponse response = client.listVolumes(listVolumesRequest);
System.out.println(response);
}
}
查询磁盘详情
通过磁盘ID可以获取对应磁盘的详细信息,以下代码可以查询磁盘详情:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.GetVolumeRequest;
import com.baidubce.services.bcc.model.volume.GetVolumeResponse;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
GetVolumeRequest getVolumeRequest = new GetVolumeRequest()
.withVolumeId("v-ePrp****"); // 指定volume id
GetVolumeResponse response = client.getVolume(getVolumeRequest);
System.out.println(response);
}
}
挂载CDS磁盘
可以将未挂载的云磁盘挂载在对应的BCC实例上,以下代码将一个CDS云磁盘挂载在对应的BCC实例上:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.instance.StopInstanceRequest;
import com.baidubce.services.bcc.model.volume.AttachVolumeRequest;
public class AttachVolume {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "http://bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
AttachVolumeRequest attachVolumeRequest = new AttachVolumeRequest();
// 设置需要挂载的磁盘id
attachVolumeRequest.setVolumeId("v-***");
// 设置BCC虚机id
attachVolumeRequest.setInstanceId("i-***");
// 打印挂载路径
System.out.println(client.attachVolume(attachVolumeRequest).getVolumeAttachment().getDevice());
}
}
- CDS磁盘需要挂载在与其处在相同zone下的BCC实例上,否则将返回403错误。
- 只有磁盘状态为 Available 且实例状态为 Running 或 Stopped 时才允许挂载,否则调用此接口将返回 409 错误。
卸载CDS磁盘
可以将已挂载的磁盘从对应的BCC实例上卸载下来,以下代码可以支持卸载CDS磁盘:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.DetachVolumeRequest;
public class DetachVolume {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "http://bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk)).withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
DetachVolumeRequest detachVolumeRequest = new DetachVolumeRequest();
// 设置待卸载的磁盘对应的ID
detachVolumeRequest.setVolumeId("v-***");
// 设置所挂载的实例ID
detachVolumeRequest.setInstanceId("i-***");
// 执行卸载操作
client.detachVolume(detachVolumeRequest);
}
}
- 只有实例状态为 Running 或 Stopped 时,磁盘才可以执行此操作,否则将提示 409 错误。
- 如果 volumeId 的磁盘不挂载在 instanceId 的实例上,该操作失败,提示 404 错误。
释放CDS磁盘
如下代码可以释放未挂载的CDS磁盘:
- 可以指定是否删除磁盘关联的快照,缺省情况下,该磁盘的所有快照将保留,但会删除与磁盘的关联关系。
- 可以指定是否将磁盘放入回收站,缺省情况下默认进入回收站。
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.ReleaseVolumeRequest;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
// 释放磁盘
// 请求参数
ReleaseVolumeRequest request = new ReleaseVolumeRequest();
// 磁盘ID
request.setVolumeId("v-***");
// 是否删除磁盘关联的自动快照
request.setAutoSnapshot("on");
// 是否删除磁盘关联的手动快照
request.setManualSnapshot("on");
// 是否将磁盘放入回收站,为off时直接释放,不进入回收站
request.setRecycle("off");
client.releaseVolume(request);
}
}
- 已挂载的CDS磁盘不能释放,系统盘不能释放。
- 磁盘释放后不可恢复。缺省情况下,该磁盘的所有快照将保留,但会删除与磁盘的关联关系。
- 只有磁盘状态为 Available 或 Expired 或 Error 时才可以执行此操作,否则将提示 409 错误。
修改磁盘属性
可以使用以下代码修改指定磁盘名称、描述信息:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.ModifyCdsAttrRequest;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
ModifyCdsAttrRequest modifyCdsAttributeRequest = new ModifyCdsAttrRequest;
// 设置磁盘id
modifyCdsAttributeRequest.setCdsId("v-dj5Q****");
// 设置新的磁盘名称
modifyCdsAttributeRequest.setCdsName("volumeName");
// 设置磁盘新的描述
modifyCdsAttributeRequest.setDesc("volumeDesc");
client.modifyCdsAttribute(modifyCdsAttributeRequest);
}
}
磁盘计费变更
可以使用以下代码变更磁盘计费方式,仅支持按量付费(后付费)转包年包月(预付费)、包年包月(预付费)转按量付费(后付费)两种方式。变更为包年包月(预付费)需要指定购买时长。
通过以下代码可将包年包月(预付费)磁盘变更为按量付费(后付费):
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
// 将预付费磁盘变更为后付费磁盘
// 请求参数
ModifyVolumeChargeRequest request = new ModifyVolumeChargeRequest();
// 磁盘ID
request.setVolumeId("v-***");
// 计费方式,当前取值为postpay, 可不填
request.setBillingMethod("postpay");
client.modifyVolumeChargeType(request);
}
}
通过以下代码可以将按量付费(后付费)磁盘变更为包年包月(预付费):
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
// 将后付费磁盘变更为预付费磁盘
// 请求参数
ModifyVolumeChargeRequest request = new ModifyVolumeChargeRequest();
// 磁盘ID
request.setVolumeId("v-***");
// 计费方式,当前取值为prepay, 可不填
request.setBillingMethod("prepay");
// 变更为prepay时,指定预付费时长,当前支持付费单位为月,取值范围为1-9
request.setReservationLength(1);
client.modifyVolumeChargeType(request);
}
}
磁盘扩容
使用以下代码可以对磁盘进行扩大容量操作:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.ResizeVolumeRequest;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
// 请求变更磁盘类型和大小
ResizeVolumeRequest request = new ResizeVolumeRequest();
// 磁盘ID
request.setVolumeId("v-***");
// 磁盘大小, 单位为GB
request.setNewCdsSizeInGB(50);
// 磁盘类型, 取值范围见https://cloud.baidu.com/doc/BCC/s/6jwvyo0q2#storagetype
request.setNewVolumeType("cloud_hp1");
client.resizeVolume(request);
}
}
磁盘只能进行扩大容量,不支持缩小容量。 只有Available状态的磁盘,才能进行扩容操作。 磁盘扩容是一个异步接口,可通过查询磁盘详情接口查询磁盘扩容状态。
回滚磁盘
可以使用指定磁盘自身的快照回滚磁盘内容,使用以下代码可以对磁盘进行回滚:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.RollbackVolumeRequest;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
RollbackVolumeRequest rollbackVolumeRequest = new RollbackVolumeRequest();
// 设置磁盘id
rollbackVolumeRequest.setVolumeId("v-zBms****");
// 设置磁盘的快照id
rollbackVolumeRequest.setSnapshotId("s-HkbJ****");
// 执行回滚操作
client.rollbackVolume(rollbackVolumeRequest);
}
}
- 磁盘状态必须为 Available 才可以执行回滚磁盘操作。
- 指定快照id必须是由该磁盘ID创建的快照。
- 若是回滚系统盘,实例状态必须为 Running 或 Stopped 才可以执行此操作。
- 回滚系统盘快照,自本次快照以来的系统盘数据将全部丢失,不可恢复。
磁盘续费
对磁盘的续费操作,可以延长过期时长,以下代码可以对磁盘进行续费:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.Billing;
import com.baidubce.services.bcc.model.Reservation;
import com.baidubce.services.bcc.model.volume.PurchaseReservedVolumeRequest;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
PurchaseReservedVolumeRequest purchaseReservedVolumeRequest = new PurchaseReservedVolumeRequest();
// 设置磁盘id
purchaseReservedVolumeRequest.setVolumeId("v-dj5Q****");
// 设置billing信息
Billing billing = new Billing();
// 设置续费时长,单位目前只支持'月'为单位
billing.setReservation(new Reservation().withReservationLength(1));
billing.setPaymentTiming("Prepaid");
// 设置billing信息
purchaseReservedVolumeRequest.setBilling(billing);
// 执行续费磁盘操作
client.purchaseReservedVolume(purchaseReservedVolumeRequest);
}
}
磁盘开通自动续费
如下代码可以为指定磁盘开通自动续费:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.AutoRenewVolumeRequest;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
AutoRenewVolumeRequest req = new AutoRenewVolumeRequest();
// 磁盘ID
req.setVolumeId("v-***");
// 续费时间单位, 当前支持取值为:month
req.setRenewTimeUnit("month");
// 续费时间,以month为单位时,取值为1-9
req.setRenewTime(3);
client.autoRenewVolume(req);
}
}
}
- 注意数据盘才能进行自动续费操作
- 注意过期状态磁盘不能进行自动续费操作
- 异步接口,可通过查询磁盘详情接口查询磁盘到期时间
磁盘取消自动续费
如下代码可以为指定磁盘取消自动续费
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.CancelAutoRenewVolumeRequest;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
CancelAutoRenewVolumeRequest req = new CancelAutoRenewVolumeRequest();
req.setVolumeId("v-***"); // 磁盘ID
client.cancelAutoRenewVolume(req);
}
}
- 注意数据盘才能进行取消自动续费操作
- 异步接口,可通过查询磁盘详情接口查询磁盘到期时间
磁盘重命名
如下代码可以用于修改指定磁盘名称:
import com.baidubce.BceClientConfiguration;
import com.baidubce.Protocol;
import com.baidubce.auth.DefaultBceCredentials;
import com.baidubce.services.bcc.BccClient;
import com.baidubce.services.bcc.BccClientConfiguration;
import com.baidubce.services.bcc.model.volume.RenameVolumeRequest;
public class Main {
public static void main(String[] args) {
// 设置您的ak、sk和要访问的endpoint
String endpoint = "bcc.bj.baidubce.com";
String ak = "ak";
String sk = "sk";
// 设置默认配置
BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
.withProtocol(Protocol.HTTP)
.withCredentials(new DefaultBceCredentials(ak, sk))
.withEndpoint(endpoint);
// 创建bcc client
BccClient client = new BccClient(bccClientConfiguration);
RenameVolumeRequest renameVolumeRequest = new RenameVolumeRequest();
// 待重命名的磁盘ID
renameVolumeRequest.setVolumeId("v-1YgO****");
// 磁盘新的名称,自定义镜像名称,支持大小写字母、数字、中文以及-_ /.特殊字符,必须以字母开头,长度1-65。
renameVolumeRequest.setName("newName");
client.renameVolume(renameVolumeRequest);
}
}