This topic describes how to use OSS SDK for PHP to download specific objects to local files.

The following code provides an example on how to download a specific object to a local file:

<? php
if (is_file(__DIR__ . '/../autoload.php')) {
    require_once __DIR__ . '/../autoload.php';
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
    require_once __DIR__ . '/../vendor/autoload.php';

use OSS\OssClient;

// Security risks may arise if you log on with the AccessKey pair of an Alibaba Cloud account because the account has permissions on all API operations. We recommend that you use your RAM user's credentials to call API operations or perform routine operations and maintenance. To create a RAM user, log on to the RAM console.
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// The endpoint of the China (Hangzhou) region is used in this example. Specify the actual endpoint.
$endpoint = "";
$bucket= "<yourBucketName>";
// <yourObjectName> indicates the complete path of the object you want to download from OSS. The path must include the extension of the object name. Example: abc/123.jpg.
$object = "<yourObjectName>";
// <yourLocalFile> consists of a local file path and a file name with an extension. Example: /users/local/myfile.txt.
$localfile = "<yourLocalFile>";
$options = array(
        OssClient::OSS_FILE_DOWNLOAD => $localfile

// Use try catch to catch exceptions. If an exception is returned, the download fails. If no exceptions are returned, the object is downloaded.
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

    $ossClient->getObject($bucket, $object, $options);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
print(__FUNCTION__ . ": OK, please check localfile: 'upload-test-object-name.txt'" . "\n");