Initialization
Confirm Endpoint
When confirming the Endpoint configured when you use the SDK, you can first read the section on BLB Access Domain Name in the Developer Guide to understand the Endpoint concept. Baidu AI Cloud currently supports multi-regions. Please see Region Selection Instructions.
Currently support the regions of "Beijing", "Guangzhou", "Suzhou", "Hong Kong", "Wuhan" and "Baoding".
The corresponding information of the service domain name is:
Access region | Corresponding 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 |
New BlbClient
Being the client of the BLB service, BlbClient provides a series of methods for developers to interact with the BLB service.
Before creating a BlbClient, you need to configure the BlbClient by creating a configuration file named YourConf.php with specific content as follows:
// Report all PHP errors
error_reporting(-1);
define('__BLB_CLIENT_ROOT', dirname(__DIR__));
// Set Access Key ID, Secret Access Key and ENDPOINT of BlbClient
$BLB_TEST_CONFIG =
array(
'credentials' => array(
'accessKeyId' => 'your ak',
'secretAccessKey' => 'your sk',
'sessionToken' => 'your session token'
),
'endpoint' => 'http://blb.bj.baidubce.com',
'stsEndpoint' => 'http://sts.bj.baidubce.com',
);
// Set the format and level of the log
$__handler = new \Monolog\Handler\StreamHandler(STDERR, \Monolog\Logger:DEBUG);
$__handler->setFormatter(
new \Monolog\Formatter\LineFormatter(null, null, false, true)
);
\BaiduBce\Log\LogFactory:setInstance(
new \BaiduBce\Log\MonoLogFactory(array($__handler))
);
\BaiduBce\Log\LogFactory:setLogLevel(\Psr\Log\LogLevel:DEBUG);
Note:
- In the above codes,
ACCESS_KEY_ID
corresponds to the "Access Key ID" in the console, andSECRET_ACCESS_KEY
corresponds to the "Access Key Secret" in the console. For how to obtain them, please see <Acquire ACCESSKEYin the Operation Guide>. - If the user needs to specify the domain name by himself, it can be specified by passing in the ENDPOINT parameter which needs to be defined by the domain name of the specified region. If the service region is Beijing, it is http://blb.bj.baidubce.com.
- If STS authentication is not used, stsEndpoint and sessionToken can be empty or deleted.
Create BlbClient Using AK/SK
To access BLB through AK/SK, users can see the following code to create a BlbClient:
//Use the PHP SDK and use a custom configuration file
include 'BaiduBce.phar';
require 'YourConf.php';
use BaiduBce\BceClientConfigOptions;
use BaiduBce\Util\Time;
use BaiduBce\Util\MimeTypes;
use BaiduBce\Http\HttpHeaders;
use BaiduBce\Services\Blb\BlbClient;
//Call parameters in the configuration file
global $BLB_TEST_CONFIG;
//New BlbClient
$client = new BlbClient($BLB_TEST_CONFIG);
Create BlbClient Using STS
Apply for STS Token BLB can implement temporary authorized access by a third party through the STS mechanism. STS (Security Token Service) is a temporary authorization service provided by Baidu AI Cloud. With STS, you can issue a third-party user with a custom time-based and privileged access credential. The third-party user can use the access credential to directly call API or SDK of Baidu AI Cloud to access its resources.
To access BLB through STS, users need to apply for an authentication string through STS client first. For the application method, please see Baidu AI Cloud STS Usage Introduction.
Create BLBClient with STS Token After applying for STS, you can configure the STS token into BlbClient. Users can create BlbClient referring to the following codes:
- First perform endpoint configuration for STS. The configuration example of STS is as follows:
$BLB_TEST_CONFIG =
array(
'credentials' => array(
'accessKeyId' => 'your ak',
'secretAccessKey' => 'your sk',
),
'stsEndpoint' => 'http://sts.bj.baidubce.com',
);
- The sample code of StsClient is as follows:
//Create StsClient
$client = new StsClient($BLB_TEST_CONFIG);
$request =
array(
'acl' => $aclArray, //User-defined acl
'durationSeconds' => 43200, //Validity time of STS certificate
);
$response = $client->getSessionToken($request);
$accessKeyID= $response->accessKeyId;
$secretAccessKey= $response->secretAccessKey;
$sessionToken = $response->sessionToken;
- Write the obtained accessKeyID/secretAccessKey/sessionToken to the configuration file YourConf.php and create a BlbClient.
$BLB_TEST_CONFIG =
array(
'credentials' => array(
'accessKeyId' => 'your ak',
'secretAccessKey' => 'your sk',
'sessionToken' => 'your session token'
),
'endpoint' => 'http://blb.bj.baidubce.com',
);
//New BlbClient
$client = new BlbClient($BLB_TEST_CONFIG);
Note: When a client is configured using STS, the endpoint must be configured to http://sts.bj.baidubce.com regardless of the endpoint corresponding to the BLB service.
Configure BlbClient
The PHP SDK sets some basic parameters by default in \BaiduBce\Bce.php
. If the user wants to modify the parameter values, he can create his own parameter configuration features referring to this file and pass in them while constructing the BlbClient. The codes passed in are as follows:
public feature CustomizedConfig() {
$customizedConfig = array(
BceClientConfigOptions:PROTOCOL => 'http',
BceClientConfigOptions:REGION => 'bj',
BceClientConfigOptions:CONNECTION_TIMEOUT_IN_MILLIS => 120 * 1000,
BceClientConfigOptions:SOCKET_TIMEOUT_IN_MILLIS => 300 * 1000,
BceClientConfigOptions:SEND_BUF_SIZE => 5 * 1024 * 1024,
BceClientConfigOptions:RECV_BUF_SIZE => 5 * 1024 * 1024,
BceClientConfigOptions:CREDENTIALS => array(
'ak' => 'your-access-key-id',
'sk' => 'your-secret-access-key',
),
'endpoint' => 'your-endpoint',
);
//Create BLBClient with custom configuration
$customizedClient = new BlbClient($customizedConfig);
//Call methods through custom configuration
$options = array(BlbOptions:CONFIG=>$customizedConfig);
$this->client->listBlbs($options);
}
Parameter Description
Parameter | Description | Default value |
---|---|---|
PROTOCOL | Protocol | http |
REGION | Region | bj |
CONNECTION_TIMEOUT_IN_MILLIS | Request timeout (unit: millisecond) | 50 * 1000 |
SOCKET_TIMEOUT_IN_MILLIS | Timeout for transmitting data over open connections (unit: millisecond) | 0 refers to infinite waiting; if you set a non-zero value, you need to evaluate the file size and network speed, otherwise timeouts will occur when large files are uploaded) |
SEND_BUF_SIZE | Send buffer size | 1024 * 1024 |
RECV_BUF_SIZE | Protocol | http |
PROTOCOL | Receive buffer size | 10 1024 1024 |