Key pair
Updated at:2025-10-20
Create a key pair
The following code is used to create key pair:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 keypair_name = 'bcc' # Name of key pair to be created
15 keypair_desc = 'used for bcc' # Description of key pair to be created
16 resp = client.create_keypair(
17 keypair_name=keypair_name,
18 keypair_desc=keypair_desc)
19 print(resp)
Import a key pair
The following code is used to import key pairs:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 keypair_name = 'keyName'
15 keypair_desc = 'Desc'
16 public_key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQA***OGfME75krwo6DbUkpzJPYbBctqD'
17 resp = client.import_keypair(keypair_name=keypair_name, # Name of key pair to be created
18 keypair_desc=keypair_desc, # Description of key pair to be created
19 public_key=public_key), # Public key content
20 print(resp)
Query the key pair list
The following code is used to query the key pair list:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 resp = client.list_keypairs(
15 max_keys=1, # Maximum number of items per page, with a maximum not exceeding 1000. The default is 1,000
16. marker='k-Su1ZAg78' # Starting position of batch list query, which is a system-generated string
17 )
18 print(resp)
Query key pair details
The following code is used to query the key pair list:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 keypair_id = 'k-wgBDZ***' # Set queried key pair ID
15 resp = client.get_keypair(keypair_id=keypair_id)
16 print(resp)
Bind a key pair
The following code is used to bind a key pair to a BCC instance:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14 keypair_id = 'k-***' # Set bound key pair ID
15 instance_ids = ['i-***', 'i-***'] # Set the bound instance ID, supporting multiple
16 resp = client.attach_keypair(keypair_id=keypair_id, instance_ids=instance_ids)
17 print(resp)
Unbind a key pair
The following code is used to unbind key pairs:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set the ID of the key pair to be unbound
15 keypair_id = 'k-***'
16# List of short IDs of VMs to be bound
17 instance_ids = ['i-***', 'i-***']
18# Initiate unbinding
19 resp = client.detach_keypair(keypair_id=keypair_id, instance_ids=instance_ids)
20 print(resp)
Delete a key pair
The following code is used to delete key pairs:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set ID of key pair to be deleted
15 keypair_id = 'k-wgBDZ***'
16# Initiate a Delete request
17 client.delete_keypair(keypair_id=keypair_id)
Rename key pair
The following code is used to rename key pairs:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set ID of key pair to be renamed
15 keypair_id = 'k-wgBDZ***'
16# Set the name of key pair to be renamed
17 keypair_name = 'Name1'
18# Change the name of key pair
19 resp = client.rename_keypair(keypair_id=keypair_id, keypair_name=keypair_name)
20 print(resp)
Update description of key pair
The following code is used to update key pair description:
Python
1from baidubce.auth.bce_credentials import BceCredentials
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.services.bcc import bcc_client
4if __name__ == '__main__':
5# Set your AK, SK, and the region to be accessed
6 HOST = 'http://bcc.bj.baidubce.com'
7 AK = 'ak'
8 SK = 'sk'
9# Set default configuration
10 config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
11 endpoint=HOST)
12# Create a BCC client
13 client = bcc_client.BccClient(config)
14# Set ID of key pair whose description is to be updated
15 keypair_id = 'k-wgBDZ***'
16# Set the description of key pair to be updated
17 keypair_desc = 'change desc'
18# Update description of key pair
19 resp = client.update_keypair_desc(keypair_id=keypair_id, keypair_desc=keypair_desc)
20 print(resp)
