Key pair
Create a key pair
You can use the following code to create key pairs, with a default quota of 500 per user in a single region (the sum of created and imported key pairs). The created key pairs can be implanted into instances to enable remote login to the instances:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairCreateRequest;
7import com.baidubce.services.bcc.model.keypair.KeypairModel;
8public class CreateKeypair {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "http://bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 String keyName = "dcc";
22 String keyDes = "for dcc";
23 KeypairCreateRequest keypairCreateRequest = new KeypairCreateRequest();
24 // Set key name
25 keypairCreateRequest.setName(keyName);
26 // Set key description
27 keypairCreateRequest.setDescription(keyDes);
28 KeypairModel keypairModel = client.createKeypair(keypairCreateRequest);
29 System.out.println(keypairModel.getPublicKey());
30 }
31}
Import a key pair
You import and create key pairs can with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairImportRequest;
7import com.baidubce.services.bcc.model.keypair.KeypairModel;
8public class ImportKeypair {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "http://bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 KeypairImportRequest requst = new KeypairImportRequest();
22 // Name of created key pair
23 requst.setName("keypairFromImport");
24 // Description of key pair to be created, optional
25 requst.setDescription("self desc....");
26 // Public key of key pair to be created
27 requst.setPublicKey("ssh-***");
28 KeypairModel keypairModel = client.importKeypair(requst);
29 System.out.println(keypairModel.getPublicKey());
30 }
31}
Query the key pair list
You can query the key pair list with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairListRequest;
7import com.baidubce.services.bcc.model.keypair.KeypairListResponse;
8public class ListKeypair {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "http://bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 // Create request body
22 KeypairListRequest keypairListRequest = new KeypairListRequest();
23 // Maximum number of items included per page, maximum 1,000. The default is 1,000
24. keypairListRequest.setMaxKeys(1);
25 // Starting position of batch list query, which is a system-generated string
26 keypairListRequest.setMarker("k-gXZ7LlbS");
27 KeypairListResponse keypairListResponse = client.listKeypair(keypairListRequest);
28 System.out.println(keypairListResponse.getKeypairs());
29 }
30}
Query key pair details
You can query the detailed information of a single key pair with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairDetailRequest;
7import com.baidubce.services.bcc.model.keypair.KeypairModel;
8public class KeypairDetail {
9 public static void main(String[] args) {
10 // Set your AK, SK and the endpoint to be accessed
11 String endpoint = "http://bcc.bj.baidubce.com";
12 String ak = "ak";
13 String sk = "sk";
14 // Set default configuration
15 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
16 .withProtocol(Protocol.HTTP)
17 .withCredentials(new DefaultBceCredentials(ak, sk))
18 .withEndpoint(endpoint);
19 // Create a BCC client
20 BccClient client = new BccClient(bccClientConfiguration);
21 // Create request body
22 KeypairDetailRequest keypairDetailRequest = new KeypairDetailRequest();
23 // Set queried key pair ID
24 keypairDetailRequest.setKeypairId("k-***");
25 // Query key pair details
26 KeypairModel keypairModel = client.keypairDetail(keypairDetailRequest);
27 System.out.println(keypairModel.getPublicKey());
28 }
29}
Bind a key pair
You can bind a selected key pair (limited to a single one) with the following code to the selected BCC instances (supporting multiple). Currently, a BCC instance can only be bound to one key pair:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairAttachRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class AttachKeypair {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "http://bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient client = new BccClient(bccClientConfiguration);
22 List<String> instances = new ArrayList<String>();
23 // Set bound instance ID (supporting multiple)
24 instances.add("i-***");
25 KeypairAttachRequest keypairAttachRequest = new KeypairAttachRequest();
26 // Set bound key pair ID
27 keypairAttachRequest.setKeypairId("k-***");
28 keypairAttachRequest.setInstanceIds(instances);
29 client.attachKeypair(keypairAttachRequest);
30 }
31}
Unbind a key pair
The following instance can be used to unbind the selected BCC instances from their respective bound key pairs. This operation is only applicable to BCC instances running on the Linux system, and the selected BCC instances must be in the Running or Stopped status:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairDetachRequest;
7import java.util.ArrayList;
8import java.util.List;
9public class DetachKeypair {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "http://bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient client = new BccClient(bccClientConfiguration);
22 // Create request body
23 KeypairDetachRequest keypairDetachRequest = new KeypairDetachRequest();
24 // Bound key ID
25 keypairDetachRequest.setKeypairId("k-***");
26 List<String> a = new ArrayList<String>();
27 a.add("i-***");
28 a.add("i-***");
29 // Instance
30 keypairDetachRequest.setInstanceIds(a);
31 // Initiate unbinding
32 client.detachKeypair(keypairDetachRequest);
33 }
34}
Delete a key pair
You can delete the key pairs with the following code. Key pairs that are already bound to BCC instances cannot be deleted:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairDeleteRequest;
7public class DeleteKeypair {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "http://bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient client = new BccClient(bccClientConfiguration);
20 // Construct request body
21 KeypairDeleteRequest keypairDeleteRequest = new KeypairDeleteRequest();
22 // Deleted key ID
23 keypairDeleteRequest.setKeypairId("k-***");
24 // Initiate a Delete request
25 client.deleteKeypair(keypairDeleteRequest);
26 }
27}
Rename a key pair
You can rename a key pair with the following code:
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairRenameRequest;
7public class RenameKeypair {
8 public static void main(String[] args) {
9 // Set your AK, SK and the endpoint to be accessed
10 String endpoint = "http://bcc.bj.baidubce.com";
11 String ak = "ak";
12 String sk = "sk";
13 // Set default configuration
14 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
15 .withProtocol(Protocol.HTTP)
16 .withCredentials(new DefaultBceCredentials(ak, sk))
17 .withEndpoint(endpoint);
18 // Create a BCC client
19 BccClient client = new BccClient(bccClientConfiguration);
20 // Create request body
21 KeypairRenameRequest keypairRenameRequest = new KeypairRenameRequest();
22 // ID of key pair to be renamed
23 keypairRenameRequest.setKeypairId("k-***");
24 // Name of new key pair
25 keypairRenameRequest.setName("Name2");
26 // Initiate an update of key pair name
27 client.renameKeypair(keypairRenameRequest);
28 }
29}
Change description of key pair
You can modify the description of a key pair using the following code. If the user provides an empty new description, the existing description of the key pair will be removed.
1import com.baidubce.BceClientConfiguration;
2import com.baidubce.Protocol;
3import com.baidubce.auth.DefaultBceCredentials;
4import com.baidubce.services.bcc.BccClient;
5import com.baidubce.services.bcc.BccClientConfiguration;
6import com.baidubce.services.bcc.model.keypair.KeypairDetailRequest;
7import com.baidubce.services.bcc.model.keypair.KeypairModel;
8import com.baidubce.services.bcc.model.keypair.KeypairUpdateDescRequest;
9public class UpdateKeypairDescription {
10 public static void main(String[] args) {
11 // Set your AK, SK and the endpoint to be accessed
12 String endpoint = "http://bcc.bj.baidubce.com";
13 String ak = "ak";
14 String sk = "sk";
15 // Set default configuration
16 BceClientConfiguration bccClientConfiguration = new BccClientConfiguration()
17 .withProtocol(Protocol.HTTP)
18 .withCredentials(new DefaultBceCredentials(ak, sk))
19 .withEndpoint(endpoint);
20 // Create a BCC client
21 BccClient client = new BccClient(bccClientConfiguration);
22 // Create request body
23 KeypairUpdateDescRequest keypairUpdateDescRequest = new KeypairUpdateDescRequest();
24 // ID of the key pair whose description is to be changed
25 keypairUpdateDescRequest.setKeypairId("k-***");
26 // Description of new key pair
27 keypairUpdateDescRequest.setDescription("desc");
28 // Update description of key pair
29 client.updateKeypairDescription(keypairUpdateDescRequest);
30 }
31}
