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

The following code provides an example on how to download an OSS object to local memory and obtain the content of the object:

<? 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 use the AccessKey pair of an Alibaba Cloud account to log on to OSS, because the account has permissions on all API operations. We recommend that you use a RAM user to call API operations or perform routine operations and maintenance. To create a RAM user, visit
$accessKeyId = "<yourAccessKeyId>";
$accessKeySecret = "<yourAccessKeySecret>";
// This example uses the public endpoint of OSS in the China (Hangzhou) region. Specify the actual endpoint based on your requirements.
$endpoint = "";
$bucket= "<yourBucketName>";
// Specify the object name.
$object = "<yourObjectName>";
    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);

    $content = $ossClient->getObject($bucket, $object);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
print(__FUNCTION__ . ": OK" . "\n");
Note If a power failure occurs, data stored in the memory is lost. To store the downloaded object in a local disk, see Download to your local file.