Initialization
Confirm Endpoint
Before configuring the endpoint for SDK usage, please refer toAPI Service Domain Name in API Reference to understand endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer toRegion Selection Guide. Corresponding information:
| Access region | Endpoint |
|---|---|
| Beijing | eip.bj.baidubce.com |
| Guangzhou | eip.gz.baidubce.com |
| Suzhou | eip.su.baidubce.com |
| Hong Kong | eip.hkg.baidubce.com |
| Wuhan | eip.fwh.baidubce.com |
| Baoding | eip.bd.baidubce.com |
Retrieve access key
To use Baidu AI Cloud EIP, users need a valid AK (Access Key ID) and SK (Secret Access Key) for signature certification. AK/SK are system-assigned strings used to identify users and perform signature certification for EIP. Your AK/SK information can be obtained and understood through the following steps: Register a Baidu AI Cloud account Create AK/SK
Create EipClient/EipGroupClient/EipBpClient/EipTpClient
EipClient/EipGroupClient/EipBpClient/EipTpClient is the Python client for EIP services, enabling developers to interact with EIP services using a variety of provided methods.
Create EipClient/EipGroupClient/EipBpClient/EipTpClient with AK/SK
- Before creating EipClient/EipGroupClient, a configuration file must be created for the purpose of configuring EipClient/EipGroupClient/EipBpClient/EipTpClient. In the example below, this file is named eip_sample_conf.py, with the following configuration details:
1#!/usr/bin/env python
2#coding=utf-8
3# Import Python standard logging module
4import logging
5# Import EIP configuration management module and security authentication module from Python SDK
6from baidubce.bce_client_configuration import BceClientConfiguration
7from baidubce.auth.bce_credentials import BceCredentials
8# Set EipClient Host, Access Key ID, and Secret Access Key
9eip_host = "eip.bj.baidubce.com"
10access_key_id = "AK"
11secret_access_key = "SK"
12# Set log file handles and log levels
13logger = logging.getLogger('baidubce.http.bce_http_client')
14fh = logging.FileHandler("sample.log")
15fh.setLevel(logging.DEBUG)
16# Set the order, structure and content of log file output
17formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
18fh.setFormatter(formatter)
19logger.setLevel(logging.DEBUG)
20logger.addHandler(fh)
21# Create BceClientConfiguration instance
22config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint = eip_host)
Note: For log files, Logging are available the following levels: DEBUG, INFO, WARNING, ERROR, 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 the Guide - Manage ACCESSKEY.
The method above requires users to specify the EIP server domain name by themselves, which can be assigned to the eip_host variable. If not specified, there is no need to pass the endpoint parameter; the default is the Beijing region http://eip.bj.baidubce.com.
- After completing the above configuration, refer to the following code to create an EipClient/EipGroupClient/EipBpClient/EipTpClient.
1# Import the EipClient configuration file
2import eip_sample_conf
3
4# Import Eip-related modules
5from baidubce import exception
6from baidubce.services import eip
7from baidubce.services.eip.eip_client import EipClient
8from baidubce.services.eip.eip_group_client import EipGroupClient
9from baidubce.services.eip.eip_bp_client import EipBpClient
10from baidubce.services.eip.eip_tp_client import EipTpClient
11# Create EipClient
12eip_client = EipClient(eip_sample_conf.config)
13 #Create EipGroupClient
14eip_group_client = EipGroupClient(eip_sample_conf.config)
15 #Create EipBpClient
16eip_bp_client = EipBpClient(eip_sample_conf.config)
17 #Create EipTpClient
18eip_tp_client = EipTpClient(eip_sample_conf.config)
Create EipClient/EipGroupClient/EipBpClient/EipTpClient with STS
EIP allows temporary third-party access authorization using the STS mechanism. STS (Security Token Service) is a temporary authorization service offered by Baidu AI Cloud. With STS, you can issue access credentials with customizable validity periods and permissions to third-party users. These users can then utilize the credentials to directly call Baidu AI Cloud APIs or SDKs to access cloud resources.
To access EIP via STS, users must first request a certification string through the STS client. For instructions on obtaining STS credentials, refer to Baidu AI Cloud STS Usage Guide.
- Obtain a temporary token and create a configuration file named "sts_sample_conf.py" for setting up the EipClient/EipGroupClient/EipBpClient/EipTpClient.
1import logging
2from baidubce.bce_client_configuration import BceClientConfiguration
3from baidubce.auth.bce_credentials import BceCredentials
4sts_host = "STS_HOST"
5access_key_id = "AK"
6secret_access_key = "SK"
7logger = logging.getLogger('baidubce.services.sts.stsclient')
8fh = logging.FileHandler("sample.log")
9fh.setLevel(logging.DEBUG)
10formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
11fh.setFormatter(formatter)
12logger.setLevel(logging.DEBUG)
13logger.addHandler(fh)
14config = BceClientConfiguration(credentials=BceCredentials(access_key_id, secret_access_key), endpoint=sts_host)
Note: Currently, when configuring a client with STS, regardless of where the corresponding EIP service endpoint is located, the endpoint must be set to
http://sts.bj.baidubce.com.
- Create a new StsClient and configure BceClientConfiguration using the obtained temporary token. Create a file "sts_sample.py" to apply for a set of AK, SK and token through StsClient.
1import sts_sample_conf
2from baidubce.services.sts.sts_client import StsClient
3from baidubce.bce_client_configuration import BceClientConfiguration
4from baidubce.auth.bce_credentials import BceCredentials
5sts_client = StsClient(sts_sample_conf.config)
6duration_seconds = 3600
7# you can specify limited permissions with ACL
8access_dict = {}
9access_dict["service"] = "your service words"
10access_dict["region"] = "bj"
11access_dict["effect"] = "Allow"
12resource = ["*"]
13access_dict["resource"] = resource
14permission = ["READ"]
15access_dict["permission"] = permission
16access_control_list = {"accessControlList": [access_dict]}
17# Create StsClient
18response = sts_client.get_session_token(acl=access_control_list, duration_seconds=duration_seconds)
19sts_ak = str(response.access_key_id)
20sts_sk = str(response.secret_access_key)
21token = response.session_token
22eip_host = "EIP_HOST"
23# Configure BceClientConfiguration instance
24config = BceClientConfiguration(credentials=BceCredentials(sts_ak, sts_sk), endpoint = eip_host, security_token=token)
- After completing the above configuration, refer to the following code to create an EipClient/EipGroupClient/EipBpClient/EipTpClient.
1 import sts_sample
2
3# Import EIP-related modules
4 from baidubce import exception
5 from baidubce.services import eip
6 from baidubce.services.eip.eip_client import EipClient
7 from baidubce.services.eip.eip_group_client import EipGroupClient
8 from baidubce.services.eip.eip_bp_client import EipBpClient
9 from baidubce.services.eip.eip_tp_client import EipTpClient
10
11# Create EipClient
12 eip_client = EipClient(sts_sample.config)
13 #Create EipGroupClient
14 eip_group_client = EipGroupClient(sts_sample.config)
15 #Create EipBpClient
16 eip_bp_client = EipBpClient(sts_sample.config)
17 #Create EipTpClient
18 eip_tp_client = EipTpClient(eip_sample_conf.config)
Configure HTTPS protocol to access EIP/EIPGroup/EipBp/EipTp
EIP supports HTTPS transport protocol. You can use HTTPS to access the EIP service in the EIP Python SDK in the following two ways:
- Specify HTTPS in the endpoint:
1 config = bce_client_configuration.BceClientConfiguration(
2 credentials = bce_credentials.BceCredentials(
3 access_key_id = 'your-ak',
4 secret_access_key = 'your-sk'
5 ),
6 endpoint = 'https://eip.bj.baidubce.com'
7 )
8 eip_client = eip_client.EipClient(config)
9 eip_group_client = eip_group_client.EipGroupClient(config)
10 eip_bp_client = eip_bp_client.EipBpClient(config)
11 eip_tp_client = eip_tp_client_EipTpClient(config)
- Set the HTTPS protocol by specifying
httpsinprotocol:
1config = bce_client_configuration.BceClientConfiguration(
2 credentials = bce_credentials.BceCredentials(
3 access_key_id = 'your-ak',
4 secret_access_key = 'your-sk'
5 ),
6 endpoint = 'eip.bj.baidubce.com',
7 protocol = baidubce.protocol.HTTPS
8)
9eip_client = eip_client.EipClient(config)
10eip_group_client = eip_group_client.EipGroupClient(config)
11eip_bp_client = eip_bp_client.EipBpClient(config)
12eip_tp_client = eip_tp_client_EipTpClient(config)
Note: If you specify the protocol parameter while specifying the scheme of the endpoint, the endpoint shall prevail.
1config = bce_client_configuration.BceClientConfiguration(
2 credentials = bce_credentials.BceCredentials(
3 access_key_id = 'your-ak',
4 secret_access_key = 'your-sk'
5 ),
6 endpoint = 'http://bj.bcebos.com',
7 protocol = baidubce.protocol.HTTPS
8)
9# All in HTTP type
10eip_client = eip_client.EipClient(config)
11eip_group_client = eip_group_client.EipGroupClient(config)
12eip_bp_client = eip_bp_client.EipBpClient(config)
13eip_tp_client = eip_tp_client_EipTpClient(config)
Configure EipClient/EipGroupClient/EipBpClient
Set network parameters
Users can set some network parameters:
1# Set request timeout duration
2eip_sample_conf.config.connection_timeout_in_mills = TIMEOUT
3
4# Set receive buffer size
5eip_sample_conf.config.recv_buf_size(BUF_SIZE)
6
7# Set send buffer size
8eip_sample_conf.config.send_buf_size(BUF_SIZE)
9
10# Set connection retry policy
11# Three-time exponential backoff retry
12eip_sample_conf.config.retry_policy = BackOffRetryPolicy()
13# No retry
14eip_sample_conf.config.retry_policy = NoRetryPolicy()
