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
EipClient/EipGroupClient serves as the PHP client for the Eip/EIPGROUP/EipBp services, providing developers with methods to interact with these PHP client services.
Before creating EipClient/EipGroupClient/EipBpClient, a configuration file must be created for the purpose of configuration. In the example below, this file is named YourConf.php, with the following configuration details:
1//Report all PHP errors
2error_reporting(-1);
3define('__EIP_CLIENT_ROOT', dirname(__DIR__));
4 //Set EipClient Access Key ID, Secret Access Key and ENDPOINT
5$EIP_TEST_CONFIG =
6 array(
7 'credentials' => array(
8 'accessKeyId' => 'your ak',
9 'secretAccessKey' => 'your sk',
10 'sessionToken' => 'your session token'
11 ),
12 'endpoint' => 'http://eip.bj.baidubce.com',
13 'stsEndpoint' => 'http://sts.bj.baidubce.com',
14 );
15 //Set the log format and level
16$__handler = new \Monolog\Handler\StreamHandler(STDERR, \Monolog\Logger::DEBUG);
17$__handler->setFormatter(
18 new \Monolog\Formatter\LineFormatter(null, null, false, true)
19);
20\BaiduBce\Log\LogFactory::setInstance(
21 new \BaiduBce\Log\MonoLogFactory(array($__handler))
22);
23\BaiduBce\Log\LogFactory::setLogLevel(\Psr\Log\LogLevel::DEBUG);
Note:
- In the code above,
accessKeyIdcorresponds to “Access Key ID” in the console.secretAccessKeycorresponds to “Access Key Secret” in the console. For the method to retrieve them, refer to the Guide - Manage ACCESSKEY.- If users need to specify their own domain name, they can upload the ENDPOINT parameters, which must be defined with the domain name of the specified region. For example, if the service is located in Beijing, the endpoint will be
http://eip.bj.baidubce.com.- If STS authentication is not used,
stsEndpointandsessionTokencan be left empty or deleted.
Create EipClient/EipGroupClient/EipBpClient with AK/SK
Users can refer to the following code to create an EipClient to access BOS with AK/SK:
1//Use PHP SDK with a custom configuration file
2include 'BaiduBce.phar';
3require 'YourConf.php';
4use BaiduBce\BceClientConfigOptions;
5use BaiduBce\Util\Time;
6use BaiduBce\Util\MimeTypes;
7use BaiduBce\Http\HttpHeaders;
8use BaiduBce\Services\Eip\EipClient;
9use BaiduBce\Services\EipGroup\EipGroupClient;
10 //Call parameters from configuration file
11global $EIP_TEST_CONFIG;
12 // Create EipClient
13$eipClient = new EipClient($EIP_TEST_CONFIG);
14 // Create EipGroupClient
15$eipGroupClient = new EipGroupClient($EIP_TEST_CONFIG);
16 // Create EipBpClient
17 $eipBpClient = new Create EipBpClient($EIP_TEST_CONFIG);
Create EipClient/EipGroupClient/EipBpClient with STS
Request STS Token
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.
Create EipClient with STS Token
Upon successful application of the STS, it is possible to configure the STS token in EipClient/EipGroupClient. Users may refer to the following code to create a new EipClient/EipGroupClient:
- First, configure the STS endpoint. Below is an example of STS configuration:
1$EIP_TEST_CONFIG =
2 array(
3 'credentials' => array(
4 'accessKeyId' => 'your ak',
5 'secretAccessKey' => 'your sk',
6 ),
7 'stsEndpoint' => 'http://sts.bj.baidubce.com',
8 );
- The sample code of StsClient is as follows:
1//Create StsClient
2$client = new StsClient($EIP_TEST_CONFIG);
3$request =
4 array(
5 'acl' => $aclArray, //User-defined acl
6 'durationSeconds' => 43200, //STS credential validity period
7 );
8$response = $client->getSessionToken($request);
9$accessKeyID= $response->accessKeyId;
10$secretAccessKey= $response->secretAccessKey;
11$sessionToken = $response->sessionToken;
Note: The acl here refers to the user-defined access control list, and its syntax can be referred to in Access Control.
- Enter the retrived accessKeyID/secretAccessKey/sessionToken in the configuration file
YourConf.php, and create EipClient.
1$EIP_TEST_CONFIG =
2 array(
3 'credentials' => array(
4 'accessKeyId' => 'your ak',
5 'secretAccessKey' => 'your sk',
6 'sessionToken' => 'your session token'
7 ),
8 'endpoint' => 'http://eip.bj.baidubce.com',
9 );
10 // Create EipClient
11 $eipClient = new EipClient($EIP_TEST_CONFIG);
12 // Create EipGroupClient
13 $eipGroupClient = new EipGroupClient($EIP_TEST_CONFIG);
14 // Create EipBpClient
15 $eipBpClient = new EipBpClient($EIP_TEST_CONFIG);
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.
Configure HTTPS protocol to access EIP/EipGroup/EipBp
EIP supports HTTPS transport protocol. Users can use HTTPS to access the EIP/EipGroup service in the EIP PHP SDK in the following two ways:
- Specify HTTPS in the endpoint:
1$EIP_CONFIG =
2 array(
3 'credentials' => array(
4 'ak' => 'your-ak',
5 'sk' => 'your-sk',
6 ),
7 'endpoint' => 'https://eip.bj.baidubce.com',
8 );
9 $eipClient = new EipClient($EIP_CONFIG);
10 $eipGroupClient = new EipGroupClient($EIP_CONFIG);
11 $eipBpClient = new EipBpClient($EIP_CONFIG);
- Set the HTTPS protocol by specifying
httpsinprotocol:
1$EIP_CONFIG =
2 array(
3 'credentials' => array(
4 'ak' => 'your-ak',
5 'sk' => 'your-sk',
6 ),
7 'endpoint' => 'eip.bj.baidubce.com',
8 'protocol' => 'https',
9 );
10 $eipClient = new EipClient($EIP_CONFIG);
11 $eipGroupClient = new EipGroupClient($EIP_CONFIG);
12 $eipBpClient = new EipBpClient($EIP_CONFIG);
Note: If you specify the protocol parameter while specifying the scheme of the endpoint, the endpoint shall prevail.
1$EIP_CONFIG =
2 array(
3 'credentials' => array(
4 'ak' => 'your-ak',
5 'sk' => 'your-sk',
6 ),
7 'endpoint' => 'http://eip.bj.baidubce.com',
8 'protocol' => 'https',
9 );
10 $eipClient = new EipClient($EIP_CONFIG); //Accesses EIP via HTTP
11 $eipGroupClient = new EipGroupClient($EIP_CONFIG); //Accesses eipgroup via HTTP
12 $eipBpClient = new EipBpClient($EIP_CONFIG); //Accesses eipBp via HTTP
Access EIP/EipGroup/EipBp via custom domain name (CNAME)
Users can access the EIP service using a custom domain name in the EIP PHP SDK in the following way:
Specify the custom domain name in the endpoint and set the custom field to true to indicate the use of a custom domain name:
1$EIP_CONFIG =
2 array(
3 'credentials' => array(
4 'ak' => 'your-ak',
5 'sk' => 'your-sk',
6 ),
7 'endpoint' => 'http://custom-domain.com',
8 'custom' => true,
9 );
10 $eipClient = new EipClient($EIP_CONFIG);
11 $eipGroupClient = new EipGroupClient($EIP_CONFIG);
12 $eipBpClient = new EipBpClient($EIP_CONFIG);
Configure EipClient/EipGroupClient/EipBpClient
Set custom parameters
PHP SDK sets some default parameters in \BaiduBce\Bce.php. To modify these values, users may create custom parameter configuration functions with reference to this file, and pass them during EipClient construction. Reference code is as follows:
1public function CustomizedConfig() {
2 $customizedConfig = array(
3 BceClientConfigOptions::PROTOCOL => 'http',
4 BceClientConfigOptions::REGION => 'bj',
5 BceClientConfigOptions::CONNECTION_TIMEOUT_IN_MILLIS => 120 * 1000,
6 BceClientConfigOptions::SOCKET_TIMEOUT_IN_MILLIS => 300 * 1000,
7 BceClientConfigOptions::SEND_BUF_SIZE => 5 * 1024 * 1024,
8 BceClientConfigOptions::RECV_BUF_SIZE => 5 * 1024 * 1024,
9 BceClientConfigOptions::CREDENTIALS => array(
10 'ak' => 'your-access-key-id',
11 'sk' => 'your-secret-access-key',
12 ),
13 'endpoint' => 'your-endpoint',
14 );
15 //Create EipClient with custom configuration
16 $customizedEipClient = new EipClient($customizedConfig);
17 //Create EipGroupClient with custom configuration
18 $customizedEipGroupClient = new EipGroupClient($customizedConfig);
19 //Create EipGroupClient with custom configuration
20 $customizedEipBpClient = new EipBpClient($customizedConfig);
21 }
The parameter description is as follows:
| Parameters | Description | Default value |
|---|---|---|
| PROTOCOL | Protocol | http |
| REGION | Region | bj |
| CONNECTION_TIMEOUT_IN_MILLIS | Request timeout duration (unit: millisecond) | 50 * 1000 |
| SOCKET_TIMEOUT_IN_MILLIS | Timeout for socket data transmission (unit: ms) | 0 (infinite waiting. if a non-zero value is set, file size and network speed shall be evaluated; otherwise, uploading large files may cause timeout.) |
| SEND_BUF_SIZE | Send buffer size | 1024 * 1024 |
| RECV_BUF_SIZE | Receive buffer size | 10 1024 1024 |
