Creating and Configuring an OBS Client
If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see API Reference.
Scenarios
This section describes how to create and configure an OBS client.
Prerequisites
Before initializing the client, ensure that:
- You have selected a proper SDK version.
- You have prepared the service and development environments.
- You have downloaded and installed the SDK.
Configuring Parameters
| Parameter | Description | Recommended Value |
|---|---|---|
| key | AK | N/A |
| secret | SK | N/A |
| endpoint | Endpoint for accessing OBS, which contains the protocol type, domain name (or IP address), and port name. For example, https://your-endpoint:443. For security purposes, you are advised to use HTTPS. | N/A |
| ssl_verify | Whether to verify server-side certificates. Possible values are:
The default value is false. | N/A |
| max_retry_count | Maximum number of retries when an HTTP/HTTPS connection is abnormal. The default value is 3. | [1, 5] |
| socket_timeout | Timeout duration for transmitting data at the socket layer, in seconds. The default value is 60. | [10, 60] |
| connect_timeout | Timeout period for establishing an HTTP/HTTPS connection, in seconds. The default value is 60. | [10, 60] |
| chunk_size | Block size for reading socket streams, in bytes. The default value is 65536. | Default value |
| is_cname | Whether to use a user-defined domain name to access OBS. The default value is false. | N/A |
- Parameters whose recommended value is N/A need to be set according to the actual conditions.
- If the network is unstable, you are advised to set larger values for socket_timeout and connect_timeout.
- If the endpoint you specified does not contain a protocol, HTTPS is used by default.
By Using the Constructor
<?php
// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an ObsClient instance.
$obsClient = new ObsClient([
// Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
//Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
'key' => getenv('ACCESS_KEY_ID'),
'secret' => getenv('SECRET_ACCESS_KEY'),
// (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
// 'security_token' => getenv('SECURITY_TOKEN'),
'endpoint' => 'https://your-endpoint',
// You are advised to configure a proper timeout period and SSL verification policy in the production environment.
'timeout' => 60,
'ssl_verify' => true
]);
// Use ObsClient to access OBS.
// Close ObsClient.
$obsClient->close(); By Using the Factory Method
<?php
// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during the installation with the source code.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an ObsClient instance.
$obsClient = ObsClient::factory([
// Obtain an AK/SK pair using environment variables (recommended) or import an AK/SK pair in other ways. Using hard coding may result in leakage.
//Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/intl/en-us/usermanual-ca/ca_01_0003.html.
'key' => getenv('ACCESS_KEY_ID'),
'secret' => getenv('SECRET_ACCESS_KEY'),
// (Optional) If you use a temporary AK/SK pair and a security token to access OBS, you are not advised to use hard coding, which may result in information leakage. You can obtain an AK/SK pair using environment variables or import an AK/SK pair in other ways.
// 'security_token' => getenv('SECURITY_TOKEN'),
'endpoint' => 'https://your-endpoint',
]);
// Use ObsClient to access OBS.
// Close ObsClient.
$obsClient->close();
- The project can contain one or more instances of ObsClient.
- After you call the ObsClient -> close method to close an instance of ObsClient, the instance cannot be used anymore.
What is your overall rating for this page?
Thank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot