All Products
Document Center

Object Storage Service:Download objects as files by using OSS SDK for PHP

Last Updated:Mar 11, 2024

This topic describes how to download an Object Storage Service (OSS) object from a bucket to your computer.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.

  • To download an object as a local file, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.


The following sample code provides an example on how to download the exampleobject.txt object from the testfolder directory in the examplebucket bucket to D:\localpath. After the object is downloaded, the object is named examplefile.txt.

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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;

// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured. 
$provider = new EnvironmentVariableCredentialsProvider();
// 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 the endpoint to 
$endpoint = "";
// Specify the name of the bucket. Example: examplebucket. 
$bucket= "examplebucket";
// Specify the full path of the object. Do not include the bucket name in the full path. Example: testfolder/exampleobject.txt. 
$object = "testfolder/exampleobject.txt";
// Download the object to D:\\localpath as a local file named examplefile.txt. If a file that has the same name already exists, the downloaded object overwrites the file. Otherwise, the downloaded object is saved in the path. 
// If you do not specify the local path for the downloaded object, 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 object fails to be downloaded. If no exceptions are caught, the object is downloaded. 
    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    $ossClient = new OssClient($config);

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


How do I rename an object when I download the object?

You can use the $localfile parameter in the preceding sample code. For example, if you want to rename the downloaded object exampleobject.txt to examplefile.txt when you download the object from a bucket to a specified local path, set $localfile to D:\\localpath\\examplefile.txt.


  • For the complete sample code that is used to download an object as a local file, visit GitHub.

  • For more information about the API operation that you can call to download an object as a local file, see GetObject.