• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (PHP SDK)
  4. Quick Start
  5. General Examples of ObsClient

General Examples of ObsClient

Each time you call an API in an instance of ObsClient, you need to pass the associative array to the request as the input (excluding ObsClient->listBuckets). For a bucket-related API, the Bucket field contained in the associative array is used to specify the bucket name. 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.

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 third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

// 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\Common\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' => 'objectkey'
       // 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\Common\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 third-party open source libraries.
require 'vendor/autoload.php';
// Import the SDK code library.
require 'obs-autoloader.php';
// Declare the namespace.
use Obs\S3\ObsClient;
// Create an instance of ObsClient.
$obsClient = new ObsClient ( [ 
       'key' => '*** Provide your Access Key ***',
       'secret' => '*** Provide your Secret Key ***',
       'endpoint' => 'https://yourdomainname'
] );

// 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' => 'objectkey'
       // 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();