Help Center> Object Storage Service> PHP> Quick Start> General Examples of ObsClient
Updated on 2023-11-09 GMT+08:00

General Examples of ObsClient

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.

Each time you call an API of ObsClient, you need to pass the associative array to the request as the input. For a bucket-related API, the Bucket field contained in the associative array is used to specify the bucket name (excluding ObsClient->listBuckets). For an object-related API, the Bucket field and Key field contained in the associative array are used to specify the bucket name and object name, respectively. ObsClient supports synchronous and asynchronous API callings. Examples are as follows:

Synchronous Method Call

If any exception is thrown when an API is called by using the synchronous method, the operation fails. Otherwise, the operation succeeds. Sample code is as follows:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient([
      //Obtain an AK/SK pair using environment variables or import the 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'),
      'endpoint' => 'https://your-endpoint',
]);

// Construct bucket request parameters.
$requestParam1 = [ 
       'Bucket' => 'bucketname' 
       // Other fields.
];

try{
       // Use the synchronous method to call a bucket-related API, such as the API for creating a bucket.
       $resp = $obsClient->createBucket ( $requestParam1 );
       // If the operation is successful, handle the API calling result.
       printf ( "RequestId:%s\n", $resp ['RequestId'] );
}catch (Obs\ObsException $obsException){       
       // If the operation fails, obtain the exception details.
       printf("ExceptionCode:%s\n", $obsException->getExceptionCode());       
       printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
}

// Construct object request parameters.
$requestParam2 = [
       'Bucket' => 'bucketname',
       'Key' => 'objectname'
       // Other fields.
];

try{
       // Use the asynchronous method to call an object-related API, such as the API for downloading an object.
       $resp = $obsClient->getObject ( $requestParam2 );
       // If the operation is successful, handle the API calling result.
       printf ( "RequestId:%s\n", $resp ['RequestId'] );
}catch (Obs\ObsException $obsException){
       // If the operation fails, obtain the exception details.
       printf("ExceptionCode:%s\n", $obsException->getExceptionCode());
       printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
}

// Close obsClient.
$obsClient -> close();

Asynchronous Method Call

In the asynchronous method call mode, the calling result is returned by the callback function. If the SDK custom exception is not null, the operation fails. Otherwise, the operation succeeds. Sample code is as follows:

// Import the dependency library.
require 'vendor/autoload.php';
// Import the SDK code library during source code installation.
// require 'obs-autoloader.php';
// Declare the namespace.
use Obs\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient([
      //Obtain an AK/SK pair using environment variables or import the 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'),
      'endpoint' => 'https://your-endpoint',
]);

// Construct bucket request parameters.
$requestParam1 = [ 
       'Bucket' => 'bucketname' 
       // Other fields.
];

// Use the asynchronous method to call a bucket-related API, such as the API for creating a bucket.
$promise1 = $obsClient->createBucketAsync ( $requestParam1 , function($obsException, $resp){
       if($obsException !== null){
              // If the operation fails, obtain the exception details.
              printf("ExceptionCode:%s\n", $obsException->getExceptionCode());
              printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
       }else{
              // If the operation is successful, handle the API calling result.
              printf ( "RequestId:%s\n", $resp ['RequestId'] );
       }
});

// Wait for the result of calling bucket-related APIs.
$promise1 -> wait();

// Construct object request parameters.
$requestParam2 = [
       'Bucket' => 'bucketname',
       'Key' => 'objectname'
       // Other fields.
];

// Use the asynchronous method to call an object-related API, such as the API for downloading an object.
$promise2 = $obsClient->getObjectAsync ( $requestParam2 , function($obsException, $resp){
       if($obsException !== null){
              // If the operation fails, obtain the exception details.
              printf("ExceptionCode:%s\n", $obsException->getExceptionCode());
              printf("ExceptionMessage:%s\n", $obsException->getExceptionMessage());
       }else{
              // If the operation is successful, handle the API calling result.
              printf ( "RequestId:%s\n", $resp ['RequestId'] );
       }
});

// Wait for the result of calling object-related APIs.
$promise2 -> wait();

// Close obsClient.
$obsClient -> close();