Initialization
Confirm Endpoint
Before confirming the Endpoint you configured when using the SDK, read the section of the Developer Guide on BOS Access Domain Name to understand Endpoint-related concepts. Baidu AI Cloud currently has opened access to multi-region support, please refer to Region Selection Description.
Currently, it supports "North China-Beijing", "South China-Guangzhou" and "East China-Suzhou". Beijing: http://bj.bcebos.com
; Guangzhou: http://gz.bcebos.com
; Suzhou: http://su.bcebos.com
.
The corresponding information is:
Access region | Corresponding Endpoint |
---|---|
BJ | bj.bcebos.com |
GZ | gz.bcebos.com |
SU | su.bcebos.com |
Get the Key
To use Baidu AI Cloud BOS, you need to have a valid AK (Access Key ID) and SK (Secret Access Key) for signature authentication. AK/SK is assigned to users by the system and is a string to identify users and verify signatures for accessing BOS. You can get and understand your AK/SK information through the following steps:
1.Register Baidu AI Cloud Account
Create BOSClient
BosClient is the client of BOS service, providing methods for developers to interact with BOS service.
Before create BosClient, first create a configuration file to configure BosClient, and in the following, configuration file is named as YourConf.php
, with specific configuration information as follows:
// Report all PHP errors
error_reporting(-1);
define('__BOS_CLIENT_ROOT', dirname(__DIR__));
// Set Access Key ID, Secret Access Key and ENDPOINT of BosClient.
$BOS_TEST_CONFIG =
array(
'credentials' => array(
'accessKeyId' => 'your ak',
'secretAccessKey' => 'your sk',
'sessionToken' => 'your session token'
),
'endpoint' => 'http://bj.bcebos.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:
1.In the codes above,
accessKeyId
corresponds to "Access Key ID" in console andsecretAccessKey
corresponds to "Access Key Secret" in console; for the way of obtaining, please see the <Operation Guide Manage ACCESSKEY>. 2.You can specify their own domain name by introducing ENDPOINT parameter, which needs to be defined with the domain name of specified region, for example, if the service region is Beijing, the domain name ishttp://bj.bcebos.com
. 3.If STS verification mode is not used,stsEndpoint
andsessionToken
can be null or deleted.
Use AK/SK to Create BosClient
Access BOS through AK/SK. You can refer to the following code to create BosClient:
//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\Bos\BosClient;
//Call parameters in the configuration file
global $BOS_TEST_CONFIG;
//Create a BOSClient.
$client = new BosClient($BOS_TEST_CONFIG);
Create BosClient with STS
Apply for STS Token
BOS 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 BOS via STS, users need to apply for an authentication string via the client of STS; for the application method, see Introduction to Uuse of Baidu AI Cloud STS.
Create a BOSClient with STS Token
After applying for STS, configure STStoken to BosClient, and users can create a BosClient by reference to the following codes:
1.First perform endpoint configuration for STS. The configuration example of STS is as follows:
$BOS_TEST_CONFIG =
array(
'credentials' => array(
'accessKeyId' => 'your ak',
'secretAccessKey' => 'your sk',
),
'stsEndpoint' => 'http://sts.bj.baidubce.com',
);
2.The sample code of StsClient is as follows:
//Create StsClient
$client = new StsClient($BOS_TEST_CONFIG);
$request =
array(
'acl' => $aclArray, //Custom acl
'durationSeconds' => 43200, //Validity time of STS certificate
);
$response = $client->getSessionToken($request);
$accessKeyID= $response->accessKeyId;
$secretAccessKey= $response->secretAccessKey;
$sessionToken = $response->sessionToken;
Note: User determines the meaning of Acl, le grammar refers to Identity and Access Manangement
3.Write the obtained accessKeyID/secretAccessKey/sessionToken to configuration file YourConf.php
, and create BosClient.
$BOS_TEST_CONFIG =
array(
'credentials' => array(
'accessKeyId' => 'your ak',
'secretAccessKey' => 'your sk',
'sessionToken' => 'your session token'
),
'endpoint' => 'http://bj.bcebos.com',
);
//Creat BosClient
$client = new BosClient($BOS_TEST_CONFIG);
Note: Currently, when STS is used to configure client, wherever the endpoint of the corresponding BOS service is, endpoint needs to be configured as
http://sts.bj.baidubce.com
.
Configure HTTPS to Access BOS
BOS supports HTTPS, and you can access to BOS service with HTTPS in BOS PHP SDK in the following 2 ways:
-
Specify HTTPS in endpoint.
$BOS_CONFIG = array( 'credentials' => array( 'ak' => 'your-ak', 'sk' => 'your-sk', ), 'endpoint' => 'https://bj.bcebos.com', ); $client = new BosClient($BOS_CONFIG);
-
Set HTTPS protocol by specifying
https
inprotocol
$BOS_CONFIG = array( 'credentials' => array( 'ak' => 'your-ak', 'sk' => 'your-sk', ), 'endpoint' => 'bj.bcebos.com', 'protocol' => 'https', ); $client = new BosClient($BOS_CONFIG);
Note: If you specify scheme of endpoint while specifying protocol parameter, endpoint should prevail.
$BOS_CONFIG = array( 'credentials' => array( 'ak' => 'your-ak', 'sk' => 'your-sk', ), 'endpoint' => 'http://bj.bcebos.com', 'protocol' => 'https', ); $client = new BosClient($BOS_CONFIG); //Access bos in http form
Access to BOS with Custom Domain Name (CNAME)
You can access to BOS service with custom domain name in BOS PHP SDK in the following ways
Specify custom domain name in endpoint, and specify custom field as true, indicating the use of custom domain name.
$BOS_CONFIG =
array(
'credentials' => array(
'ak' => 'your-ak',
'sk' => 'your-sk',
),
'endpoint' => 'http://custom-domain.com',
'custom' => true,
);
$client = new BosClient($BOS_CONFIG);
$client->putObjectFromFile(null, $objectKey, $fileName);
Note: If you create BosClient in with custom domain name in PHP SDK, it is recommended to set BucketName field as null when accessing to API interface; currently, PHP SDK does not support copyobject and uploadPartCopy interfaces when using custom domain name.
Configure BosClient
Set Custom Parameters
PHP SDK sets some parameters in \BaiduBce\Bce.php
by default; to modify the parameter value, you can refer to this file to create their own parameter configuration function, and introduce it when constructing BosClient; please see the following for introduced codes:
public function 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 BOSClient with custom configuration
$customizedClient = new BosClient($customizedConfig);
//Call methods through custom configuration
$options = array(BosOptions::CONFIG=>$customizedConfig);
$this->client->listBuckets($options);
}
The parameters are described as follows:
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 | Receive buffer size | 10 1024 1024 |
Set Optional Parameters
BOSClient encapsulates optional parameters in $options
, see introduction to specific interface use method for optional parameters of each method; with putObjectFromFile
method as an example, the setting optional parameters are realized by reference to the following codes:
//Introduce specified parameters when uploading object via files with options
file_put_contents($fileName, "test of put object from string");
$user_meta = array("x-bce-meta-key1" => "value1");
$options = array(
BosOptions::CONTENT_TYPE=>"text/plain",
BosOptions::CONTENT_MD5=>base64_encode(hash_file("md5", $fileName, true)),
BosOptions::CONTENT_LENGTH=>filesize($fileName),
BosOptions::CONTENT_SHA256=>hash_file("sha256", $fileName),
BosOptions::USER_METADATA => $user_meta,
);
$client->putObjectFromFile($bucketName, $objectKey, $fileName, $options);
Note: Do not introduce
null
to$options
, otherwise, an exception is thrown during calling.