镜像
更新时间:2024-07-02
创建自定义镜像
支持通过实例创建和通过快照创建两种方式。参考以下代码可以创建一个自定义镜像:
// 通过实例创建自定义镜像
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.image.CreateImageRequest;
import com.baidubce.services.bcc.model.volume.AttachVolumeRequest;
public class CreateImageFromInstance {
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);
CreateImageRequest createImageRequest = new CreateImageRequest();
// 设置自定义镜像名称
createImageRequest.setImageName("image04");
// 当从快照创建镜像时,此参数是指用于创建镜像的快照ID。可选参数,与 snapshotId 不同时存在,同时存在时只取instanceId
createImageRequest.setSnapshotId("m-***");
// 当从实例创建镜像时,此参数是指用于创建镜像的实例ID。可选参数,与 snapshotId 不同时存在,同时存在时只取instanceId
createImageRequest.setInstanceId("i-***");
// 设置自定义镜像是否关联实例CDS磁盘,relateCds默认为false,为true时表示关联
createImageRequest.setRelateCds(true);
System.out.println(client.createImage(createImageRequest).getImageId());
}
}
注意,创建自定义镜像,默认配额20个每账号。
查询镜像列表
如下代码可以查询有权限的镜像列表:
package com.baidu.bce.image;
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.image.ListImagesRequest;
import com.baidubce.services.bcc.model.image.ListImagesResponse;
public class ListImages {
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);
ListImagesRequest request = new ListImagesRequest();
// 镜像类型
request.withImageType("ALL");
ListImagesResponse response = client.listImages(request);
System.out.println(response);
}
}
具体的镜像类型可详细参考BCC API文档查询镜像列表
查询镜像详情
如下代码可以查询镜像详细信息:
package com.baidu.bce.image;
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.image.GetImageRequest;
import com.baidubce.services.bcc.model.image.GetImageResponse;
public class GetImage {
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);
// 镜像ID
String imageId = "m-***";
GetImageRequest request = new GetImageRequest();
request.setImageId(imageId);
GetImageResponse response = client.getImage(request);
System.out.println(response);
}
}
删除自定义镜像
如下代码可以删除一个自定义镜像:
package com.baidu.bce.image;
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.image.DeleteImageRequest;
public class DeleteImage {
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);
// 镜像ID
String imageId = "m-***";
DeleteImageRequest request = new DeleteImageRequest();
request.setImageId(imageId);
client.deleteImage(request);
}
}
跨地域复制自定义镜像
如下代码用于用户跨地域复制自定义镜像,仅限自定义镜像,公共镜像和服务集成镜像不能复制
regions如北京"bj",广州"gz",苏州"su",可多选:
package com.baidu.bce.image;
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.image.RemoteCopyImageRequest;
import java.util.Arrays;
import java.util.List;
public class RemoteCopyImage {
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);
// 镜像ID
String imageId = "m-***";
// 镜像名称
String name = "copied-image";
// 目的region
List<String> destinations = Arrays.asList("hkg");
RemoteCopyImageRequest request = new RemoteCopyImageRequest();
request.setImageId(imageId);
request.setDestRegion(destinations);
request.setName(name);
client.remoteCopyImage(request);
}
}
取消跨地域复制自定义镜像
用于取消跨地域复制自定义镜像,仅限自定义镜像,公共镜像和服务集成镜像不能复制:
package com.baidu.bce.image;
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.image.CancelRemoteCopyImageRequest;
public class CancelRemoteCopyImage {
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);
// 镜像ID
String imageId = "m-***";
CancelRemoteCopyImageRequest request = new CancelRemoteCopyImageRequest();
request.setImageId(imageId);
client.cancelRemoteCopyImage(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.image.ShareImageRequest;
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);
// 请求将镜像共享给其他账号id
ShareImageRequest shareRequest = new ShareImageRequest();
// 共享账号ID
shareRequest.setAccountId("***");
// 镜像ID
shareRequest.setImageId("m-***");
client.shareImage(shareRequest);
}
}
取消共享自定义镜像
如下代码可以取消共享用户自己的指定的自定义镜像:
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.image.UnShareImageRequest;
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);
// 取消将镜像共享给某个账户id
UnShareImageRequest unShareRequest = new UnShareImageRequest();
// 账号ID
unShareRequest.setAccountId("***");
// 镜像ID
unShareRequest.setImageId("m-***");
client.unShareImage(unShareRequest);
}
}
查询镜像已共享用户列表
如下代码可以查询镜像已共享的用户列表:
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.image.CreateImageRequest;
import com.baidubce.services.bcc.model.image.ListSharedUserResponse;
public class ListSharedUser {
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);
// 镜像id
String imageId = "m-***";
ListSharedUserResponse listSharedUserResponse = client.listSharedUser(imageId);
System.out.println(listSharedUserResponse.getUsers());
}
}
根据实例短ID批量查询OS信息(新增)
如下代码可以根据实例ID批量查询OS信息:
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.image.ListOsRequest;
import com.baidubce.services.bcc.model.image.ListOsResponse;
import com.baidubce.services.bcc.model.image.ListSharedUserResponse;
import java.util.ArrayList;
import java.util.List;
public class listOs {
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);
ListOsRequest listOsRequest = new ListOsRequest();
List<String> ids = new ArrayList<String>();
// 设置需要查询的实例ID
ids.add("i-***");
// 设置需要查询的实例ID
ids.add("i-***");
listOsRequest.setInstanceIds(ids);
ListOsResponse listOsResponse = client.listOs(listOsRequest);
System.out.println(listOsResponse.getOsInfo());
}
}
根据实例规格查询可用公共镜像
如下代码可以根据实例规格查询可用公共镜像:
public static void getAvailableImagesBySpec(BccClient bccClient) {
GetAvailableImagesBySpecRequest request = new GetAvailableImagesBySpecRequest();
request.setSpec("bcc.g4.c1m1");
bccClient.getAvailableImagesBySpec(request);
}