Install MCT-PHP-SDK
Install SDK Package
- Download PHP SDK compression pack from the Official Website.
-
Decompress the installation package, and browse SDK directory:
BaiduBce.phar ├──src │ └── BaiduBce │ ├── Auth //BCE signature related │ ├── Exception //BCE client exception │ ├── Http //BCE's Http communication related │ ├── Log //BCE log │ ├── Services │ │ └── Media //Media home directory which must be kept │ │ └── MediaClient.php //Media operation class (all operations can be completed through the MediaClient class) │ └── Util //BCE utility └──vendor //Third-party libraries
-
Add the following code to the script file and saved:
include 'BaiduBce.phar'; require 'YourConf.php';
For reference of configuration files, please see [Configure MediaClient](#Configure MediaClient).
Install MediaClient
Configure MediaClient
MediaClient is the PHP client of MCT service to provide a series of methods for the developers and MCT service.
Before create MediaClient, first create a configuration file to configure MediaClient, 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('__MEDIA_CLIENT_ROOT', dirname(__DIR__));
//Set Access Key ID, Secret Access Key and ENDPOINT of MediaClient
$MEDIA_TEST_CONFIG =
array(
'credentials' => array(
'ak' => 'your-access-key-id',
'sk' => 'your-secret-access-key',
),
'endpoint' => 'http://media.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 above code, the variables AK and SK are assigned to user by the system to identify the user and perform signature verification for accessing Media. Among them, AK corresponds to the “Access Key ID” in the console, and SK corresponds to “Access Key Secret” in the console. Please refer to [Operation Guide Manage ACCESSKEY].
2.ENDPOINT parameter can only be defined by the assigned domain name containing the Region; currently, the Media only provides one region, Beijing, so the ENDPOINT only supports domain name http://media.bj.baidubce.com
, and other domain names will be supported with the increasing of Regions.
Create MediaClient
After completing the above configuration, refer to the following code to create a MediaClient.
//Use the PHP SDK and use a custom configuration file
include 'BaiduBce.phar';
require 'YourConf.php';
use BaiduBce\Services\Media\MediaClient;
//Call parameters in the configuration file
global $MEDIA_TEST_CONFIG;
//Create MediaClient
$client = new MediaClient($MEDIA_TEST_CONFIG);
Parameter Description
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 MediaClient; 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 MediaClient with custom configuration
$customizedClient = new MediaClient($customizedConfig);
//Call methods through custom configuration
$options = array('config'=>$customizedConfig);
$this->client->listPipelines($options);
}
The parameters are described as follows:
Parameter | Description | Default value |
---|---|---|
PROTOCOL | Protocol | http |
REGION | Region | bj(Only support Beijing currently) |
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 |
Related Description
MediaClient encapsulates optional parameters in $options
, see introduction to specific interface use method for optional parameters of each method; with createPipeline
method as an example, the setting optional parameters are realized by reference to the following codes:
//Use options to pass in specified optional parameters when creating Pipeline
$options = array(
'description' => 'This is a test pipeline',
'pipelineConfig' => array(
'capacity' => 15,
),
);
$client->createPipeline($pipelineName, $sourceBucket, $targetBucket, $options);
Note: Do not introduce null
to $options
, otherwise, an exception is thrown during calling.