All Products
Search
Document Center

Object Storage Service:Download a file (PHP SDK V1)

Last Updated:Nov 29, 2025

By default, a call to the GetObject operation on a versioning-enabled bucket returns only the current version of an object.

Precautions

  • In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported 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 a file, you must have the oss:GetObject permission. For more information, see Grant custom permissions to a RAM user.

Background information

When you call the GetObject operation on a bucket, one of the following three scenarios occurs:

  • If the current version of the object in the bucket is a delete marker, OSS returns 404 Not Found.

  • If you specify an object's version ID in a query parameter, OSS returns that version of the object. If you set the version ID to "null", OSS returns the object version that has a "null" version ID.

  • If you try to retrieve a delete marker by specifying its version ID, OSS returns 405 Method Not Allowed.

Sample code

The following code shows how to download a 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\Credentials\EnvironmentVariableCredentialsProvider;
use OSS\OssClient;
use OSS\CoreOssException;

// 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 set. 
$provider = new EnvironmentVariableCredentialsProvider();
// The endpoint is set to China (Hangzhou) in this example. Set the endpoint to the one for your region.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket= "<yourBucketName>";
// Specify the full path of the object. The full path does not include the bucket name. Example: example/test.txt.
$object = "<yourObjectName>";
// Specify the version ID of the object.
$versionId = "<yourObjectVersionId>";

$config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);

try{
    // Download the object of the specified version.
    $content = $ossClient->getObject($bucket, $object, array(OssClient::OSS_VERSION_ID => $versionId));
    print($content);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}

print(__FUNCTION__ . ": OK" . "\n");        

For more information, see GetObject.