This topic describes how to download an object from a bucket to your local computer.

Sample code

The following code provides an example on how to download exampleobject.txt from the testfolder directory in examplebucket to examplefile.txt in the D:\localpath path:

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 access 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 O&M. To create a RAM user, log on to the RAM console. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set Endpoint to 
$endpoint = "";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. The full path of the object cannot contain the bucket name. Example: testfolder/exampleobject.txt. 
$object = "testfolder/exampleobject.txt";
// Download the object to D:\\localpath. After the object is downloaded, the local file is named examplefile.txt. If the specified local file exists, the object to download overwrites the file. Otherwise, a file is created. 
// If the path for the object is not specified, the downloaded object is saved to the path of the project to which the sample program belongs. 
$localfile = "D:\\localpath\\examplefile.txt";
$options = array(
        OssClient::OSS_FILE_DOWNLOAD => $localfile

// Use try-catch to catch exceptions. If an exception is caught, the download fails. If no exceptions are caught, the download succeeds. 
    $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");


  • For more information about the complete sample code that is used to download objects to local files, visit GitHub.
  • For more information about the API operation that you can call to download objects to local files, see GetObject.