密钥对
更新时间:2024-07-02
创建密钥对
如下代码可以创建密钥对,单个用户在单region默认配额500个(创建和导入的密钥对之和)创建后的密钥对可植入实例以实现远程登录实例:
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.keypair.KeypairCreateRequest;
import com.baidubce.services.bcc.model.keypair.KeypairModel;
public class CreateKeypair {
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);
String keyName = "dcc";
String keyDes = "用于dcc";
KeypairCreateRequest keypairCreateRequest = new KeypairCreateRequest();
// 设置密钥名
keypairCreateRequest.setName(keyName);
// 设置密钥描述
keypairCreateRequest.setDescription(keyDes);
KeypairModel keypairModel = client.createKeypair(keypairCreateRequest);
System.out.println(keypairModel.getPublicKey());
}
}
导入密钥对
如下代码可以导入创建密钥对:
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.keypair.KeypairImportRequest;
import com.baidubce.services.bcc.model.keypair.KeypairModel;
public class ImportKeypair {
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);
KeypairImportRequest requst = new KeypairImportRequest();
// 创建密钥对名称
requst.setName("keypairFromImport");
// 待创建的密钥对的描述,可选参数
requst.setDescription("self desc....");
// 待创建的密钥对的公钥
requst.setPublicKey("ssh-***");
KeypairModel keypairModel = client.importKeypair(requst);
System.out.println(keypairModel.getPublicKey());
}
}
查询密钥对列表
如下代码可以查询密钥对列表:
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.keypair.KeypairListRequest;
import com.baidubce.services.bcc.model.keypair.KeypairListResponse;
public class ListKeypair {
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);
// 创建请求体
KeypairListRequest keypairListRequest = new KeypairListRequest();
// 每页包含的最大数量,最大数量通常不超过1000。缺省值为1000
keypairListRequest.setMaxKeys(1);
// 批量获取列表的查询的起始位置,是一个由系统生成的字符串
keypairListRequest.setMarker("k-gXZ7LlbS");
KeypairListResponse keypairListResponse = client.listKeypair(keypairListRequest);
System.out.println(keypairListResponse.getKeypairs());
}
}
查询密钥对详情
如下代码可以查询单个密钥对的详细信息:
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.keypair.KeypairDetailRequest;
import com.baidubce.services.bcc.model.keypair.KeypairModel;
public class KeypairDetail {
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);
// 创建请求体
KeypairDetailRequest keypairDetailRequest = new KeypairDetailRequest();
// 设置被查询密钥对id
keypairDetailRequest.setKeypairId("k-***");
// 查询密钥对详情
KeypairModel keypairModel = client.keypairDetail(keypairDetailRequest);
System.out.println(keypairModel.getPublicKey());
}
}
绑定密钥对
如下代码可以将所选密钥对(限单个)绑定到所选BCC实例(支持多台),目前一台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.keypair.KeypairAttachRequest;
import java.util.ArrayList;
import java.util.List;
public class AttachKeypair {
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);
List<String> instances = new ArrayList<String>();
// 设置被绑定实例id,可多个
instances.add("i-***");
KeypairAttachRequest keypairAttachRequest = new KeypairAttachRequest();
// 设置被绑定密钥对id
keypairAttachRequest.setKeypairId("k-***");
keypairAttachRequest.setInstanceIds(instances);
client.attachKeypair(keypairAttachRequest);
}
}
解绑密钥对
如下实例可以将所选BCC实例与它们各自绑定的密钥对解绑,此操作仅适用于Linux系统的BCC实例,且所选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.keypair.KeypairDetachRequest;
import java.util.ArrayList;
import java.util.List;
public class DetachKeypair {
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);
// 创建请求体
KeypairDetachRequest keypairDetachRequest = new KeypairDetachRequest();
// 被绑密钥ID
keypairDetachRequest.setKeypairId("k-***");
List<String> a = new ArrayList<String>();
a.add("i-***");
a.add("i-***");
// 实例
keypairDetachRequest.setInstanceIds(a);
// 发起解绑
client.detachKeypair(keypairDetachRequest);
}
}
删除密钥对
如下代码可以删除密钥对,已经绑定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.keypair.KeypairDeleteRequest;
public class DeleteKeypair {
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);
// 构建请求体
KeypairDeleteRequest keypairDeleteRequest = new KeypairDeleteRequest();
// 被删密钥ID
keypairDeleteRequest.setKeypairId("k-***");
// 发起删除请求
client.deleteKeypair(keypairDeleteRequest);
}
}
重命名密钥对
如下代码可以重命名密钥对:
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.keypair.KeypairRenameRequest;
public class RenameKeypair {
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);
// 创建请求体
KeypairRenameRequest keypairRenameRequest = new KeypairRenameRequest();
// 待重命名的密钥对id
keypairRenameRequest.setKeypairId("k-***");
// 新的密钥对名称
keypairRenameRequest.setName("Name2");
// 发起更新秘钥对名称
client.renameKeypair(keypairRenameRequest);
}
}
更改密钥对描述
如下代码可以更改密钥对描述,若用户提供的新的描述内容为空,则删除密钥对之前的描述。
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.keypair.KeypairDetailRequest;
import com.baidubce.services.bcc.model.keypair.KeypairModel;
import com.baidubce.services.bcc.model.keypair.KeypairUpdateDescRequest;
public class UpdateKeypairDescription {
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);
// 创建请求体
KeypairUpdateDescRequest keypairUpdateDescRequest = new KeypairUpdateDescRequest();
// 待更改描述的密钥对id
keypairUpdateDescRequest.setKeypairId("k-***");
// 新的密钥对描述
keypairUpdateDescRequest.setDescription("desc");
// 更新keypair的描述信息
client.updateKeypairDescription(keypairUpdateDescRequest);
}
}