CaClient
Create CaClient
CaClient acts as the client for CA services, providing developers with various methods to interact with CA services.
Access CA via AK/SK
-
Before creating CaClient, a configuration file must be created to configure CaClient. Below, this configuration file is named
ca_sample_conf.py, with the following specific configuration information:#!/usr/bin/env python #coding=utf-8
#Import Python standard logging module import logging
#Import BCE configuration management module and security authentication module from Python SDK from baidubce.bce_client_configuration import BceClientConfiguration from baidubce.auth.bce_credentials import BceCredentials
#Set CaClient Host, Access Key ID, and Secret Access Key ca_host = "ca.bj.baidubce.com" access_key_id = "AK" secret_access_key = "SK"
#Set log file handles and log levels logger = logging.getLogger('baidubce.http.bce_http_client') fh = logging.FileHandler("sample.log") fh.setLevel(logging.DEBUG)
#Set the order, structure and content of log file output formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') fh.setFormatter(formatter) logger.setLevel(logging.DEBUG) logger.addHandler(fh)
#Create BceClientConfiguration instance config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint = ca_host)
Note: For log files, Logging offers the following levels: DEBUG, INFO, WARNING, ERROR, and CRITICAL.
In the code above, access_key_id corresponds to “Access Key ID” in the console. secret_access_key corresponds to “Access Key Secret” in the console. For the method to retrieve them, refer to Manage ACCESSKEY.
The method above requires users to specify the CA server domain name by themselves, which can be assigned to the ca_host variable.
-
After completing the configurations above, refer to the following code to create a CaClient.
#Import CaClient configuration file import ca_sample_conf #Import related CA modules from baidubce import exception from baidubce.services import ca from baidubce.services.ca.ca_client import CaClient #Create CaClient ca_client = CaClient(ca_sample_conf.config)
