密钥对
更新时间:2024-07-02
创建密钥对
以下代码用于创建密钥对:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
keypair_name = 'bcc' # 待创建的密钥对名称
keypair_desc = '用于bcc' # 待创建的密钥对的描述
resp = client.create_keypair(
keypair_name=keypair_name,
keypair_desc=keypair_desc)
print(resp)
导入密钥对
以下代码用于导入密钥对:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
keypair_name = 'keyName'
keypair_desc = 'Desc'
public_key = 'ssh-rsa AAAAB3NzaC1yc2EAAAADAQA***OGfME75krwo6DbUkpzJPYbBctqD'
resp = client.import_keypair(keypair_name=keypair_name, # 待创建的密钥对名称
keypair_desc=keypair_desc, # 待创建的密钥对的描述
public_key=public_key), # 公钥内容
print(resp)
查询密钥对列表
以下代码用于查询密钥对列表:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
resp = client.list_keypairs(
max_keys=1, # 每页包含的最大数量,最大数量通常不超过1000。缺省值为1000
marker='k-Su1ZAg78' # 批量获取列表的查询的起始位置,是一个由系统生成的字符串
)
print(resp)
查询密钥对详情
以下代码用于查询密钥对列表:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
keypair_id = 'k-wgBDZ***' # 设置被查询密钥对id
resp = client.get_keypair(keypair_id=keypair_id)
print(resp)
绑定密钥对
以下代码用于绑定密钥对到BCC实例:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
keypair_id = 'k-***' # 设置被绑定密钥对id
instance_ids = ['i-***', 'i-***'] # 设置被绑定实例id,可多个
resp = client.attach_keypair(keypair_id=keypair_id, instance_ids=instance_ids)
print(resp)
解绑密钥对
以下代码用于解绑密钥对:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 设置要解绑的密钥对id
keypair_id = 'k-***'
# 待解绑的虚机的短id列表
instance_ids = ['i-***', 'i-***']
# 发起解绑
resp = client.detach_keypair(keypair_id=keypair_id, instance_ids=instance_ids)
print(resp)
删除密钥对
以下代码用于删除密钥对:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 设置要删除的密钥对id
keypair_id = 'k-wgBDZ***'
# 发起删除请求
client.delete_keypair(keypair_id=keypair_id)
密钥对重命名
以下代码用于密钥对重命名:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 设置要重命名的密钥对id
keypair_id = 'k-wgBDZ***'
# 设置要重命名的密钥对名称
keypair_name = 'Name1'
# 更改秘钥对名称
resp = client.rename_keypair(keypair_id=keypair_id, keypair_name=keypair_name)
print(resp)
更新密钥对描述
以下代码用于更新密钥对描述:
from baidubce.auth.bce_credentials import BceCredentials
from baidubce.bce_client_configuration import BceClientConfiguration
from baidubce.services.bcc import bcc_client
if __name__ == '__main__':
# 设置您的ak、sk和要访问的地域
HOST = 'http://bcc.bj.baidubce.com'
AK = 'ak'
SK = 'sk'
# 设置默认配置
config = BceClientConfiguration(credentials=BceCredentials(AK, SK),
endpoint=HOST)
# 创建bcc client
client = bcc_client.BccClient(config)
# 设置要更新描述的密钥对id
keypair_id = 'k-wgBDZ***'
# 设置要更新的密钥对描述
keypair_desc = 'change desc'
# 更新密钥对描述
resp = client.update_keypair_desc(keypair_id=keypair_id, keypair_desc=keypair_desc)
print(resp)