Updated on 2023-11-09 GMT+08:00

Asynchronous Method Call

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.

All bucket- and object-related APIs provided by OBS PHP SDK can be called by asynchronous methods whose names end with Async (such as ObsClient->putObjectAsync if the synchronous method is named ObsClient->putObject). The returned result will be output to a callback function. A callback function contains an SDK custom exception and an SDK common result object in sequence. If the SDK common result object is not null, the operation fails. Otherwise, the operation succeeds.

The following code shows how to upload an object in asynchronous method call mode:

// 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'
] );

// Upload the object in asynchronous method call mode.
$promise = $obsClient->putObjectAsync ( [ 
       'Bucket' => 'bucketname',
       'Key' => 'objectname',
       'Body' => 'Hello OBS' 
], function ($obsException, $resp) {
       if ($obsException === null) {
              printf ( "RequestId:%s\n", $resp ['RequestId'] );
       } else {
              printf ( "ExceptionCode:%s\n", $obsException->getExceptionCode () );
              printf ( "ExceptionMessage:%s\n", $obsException->getExceptionMessage () );
       }
} );
$promise->wait ();

A result object (GuzzleHttp\Promise\Promise) will be returned upon an asynchronous method call. You need to call the wait method of the object to wait until the asynchronous method call is complete.