Initialization
Confirm Endpoint
Before configuring the Endpoint for SDK usage, please refer to the developer guide section on BLB Access Domain Name to understand Endpoint-related concepts. Baidu AI Cloud currently supports multiple regions. Please refer to Region Selection Guide.
Currently, supported regions include "Beijing," "Guangzhou," "Suzhou," "Hong Kong," "Wuhan," "Baoding," "Shanghai," and "Singapore.\
The corresponding information for the service domain name:
| Access region | Endpoint |
|---|---|
| bj | blb.bj.baidubce.com |
| gz | blb.gz.baidubce.com |
| su | blb.su.baidubce.com |
| hkg | blb.hkg.baidubce.com |
| fwh | blb.fwh.baidubce.com |
| bd | blb.bd.baidubce.com |
| fsh | blb.fsh.baidubce.com |
| sin | blb.sin.baidubce.com |
Create a BlbClient
BlbClient acts as the client for BLB services, offering developers a variety of methods to interact with BLB service features.
Before creating BlbClient, a configuration file must be created to configure BlbClient. Below, this configuration file is named YourConf.php, with the following specific configuration information:
1//Report all PHP errors
2error_reporting(-1);
3define('__BLB_CLIENT_ROOT', dirname(__DIR__));
4 //Set BlbClient的Access Key ID, Secret Access Key and ENDPOINT
5$BLB_TEST_CONFIG =
6 array(
7 'credentials' => array(
8 'accessKeyId' => 'your ak',
9 'secretAccessKey' => 'your sk',
10 'sessionToken' => 'your session token'
11 ),
12 'endpoint' => 'http://blb.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,
ACCESS_KEY_IDcorresponds to “Access Key ID” in the console.SECRET_ACCESS_KEYcorresponds to “Access Key Secret” in the console. For the method to retrieve them, refer to the Guide - Retrieve ACCESSKEY. - If users wish to use a custom domain name, they must upload the ENDPOINT parameters, which need to align with the domain name of the specified region. For instance, if the service is hosted in Beijing, the endpoint would be http://blb.bj.baidubce.com.
- If STS authentication is not being utilized, the stsEndpoint and sessionToken fields can remain empty or be removed.
Create a BlbClient with AK/SK
Users can refer to the following code to create a BlbClient to access BLB 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\Blb\BlbClient;
9 //Call parameters from configuration file
10global $BLB_TEST_CONFIG;
11 //Create BlbClient
12$client = new BlbClient($BLB_TEST_CONFIG);
Create BlbClient with STS
Request STS Token BLB enables temporary third-party access authorization through the STS mechanism. STS (Security Token Service) is a temporary authorization service provided by Baidu AI Cloud. Through STS, you can issue access credentials with customized validity periods and permissions to third-party users. Third-party users can use these credentials to directly call Baidu AI Cloud APIs or SDKs to access cloud resources.
To access BLB 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 BLBClient with STS Token After requesting the STS token, configure it in the BlbClient. The following codes demonstrate how to initialize the client:
- First configure the STS endpoint. STS configuration example is as follows:
1$BLB_TEST_CONFIG =
2 array(
3 'credentials' => array(
4 'accessKeyId' => 'your ak',
5 'secretAccessKey' => 'your sk',
6 ),
7 'stsEndpoint' => 'http://sts.bj.baidubce.com',
8 );
- Example code for StsClient is as follows:
1//Create StsClient
2$client = new StsClient($BLB_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;
- Enter the retrieved accessKeyID/secretAccessKey/sessionToken into the configuration file YourConf.php, and then create a BlbClient.
1$BLB_TEST_CONFIG =
2 array(
3 'credentials' => array(
4 'accessKeyId' => 'your ak',
5 'secretAccessKey' => 'your sk',
6 'sessionToken' => 'your session token'
7 ),
8 'endpoint' => 'http://blb.bj.baidubce.com',
9 );
10 //Create BlbClient
11 $client = new BlbClient($BLB_TEST_CONFIG);
Note: When using STS to configure a client, the endpoint must always be http://sts.bj.baidubce.com, irrespective of the location of the corresponding BLB service.
Configure BlbClient
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 BlbClient 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 BLBClient with custom configuration
16 $customizedClient = new BlbClient($customizedConfig);
17 //Call methods via custom configuration
18 $options = array(BlbOptions::CONFIG=>$customizedConfig);
19 $this->client->listBlbs($options);
20 }
Parameter description
| 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 | Protocol | http |
| PROTOCOL | Receive buffer size | 10 1024 1024 |
