All Products
Search
Document Center

Object Storage Service:Conditional download (PHP SDK V1)

Last Updated:Nov 29, 2025

Conditional download lets you specify one or more conditions when you download an Object Storage Service (OSS) object. The object is downloaded to local memory or a local file only if the specified conditions are met. If the conditions are not met, an error is returned and the download does not start.

Usage notes

  • 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 perform a conditional download, you must have the oss:GetObject permission. For more information, see Attach a custom policy to a RAM user.

Conditions

The following table lists the conditions that OSS supports. You can use If-Modified-Since and If-Unmodified-Since at the same time. You can also use If-Match and If-None-Match at the same time.

Parameter

Description

Method

If-Modified-Since

If the object was modified after the specified time, the object is downloaded. Otherwise, a 304 Not modified error is returned.

OssClient::OSS_IF_MODIFIED_SINCE

If-Unmodified-Since

If the object was modified at or before the specified time, the object is downloaded. Otherwise, a 412 Precondition failed error is returned.

OssClient::OSS_IF_UNMODIFIED_SINCE

If-Match

If the ETag of the OSS object matches the specified ETag, the object is downloaded. Otherwise, a 412 Precondition failed error is returned.

Note

Obtain the ETag of an object using the $ossClient->getObjectMeta method.

OssClient::OSS_IF_MATCH

If-None-Match

If the ETag of the OSS object does not match the specified ETag, the object is downloaded. Otherwise, a 304 Not modified error is returned.

OssClient::OSS_IF_NONE_MATCH

Conditionally download an object to local memory

The following code shows how to specify conditions to download an object to local memory:

<?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;

// 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 following example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "yourBucketName";
// Specify the object name. The object name is the full path of the object that does not include the bucket name, such as exampledir/exampleobject.txt.
$object = "yourObjectName";

try{
    $options = array(
        OssClient::OSS_HEADERS => array(          
          // Download the object if it was modified after Fri, 9 Apr 2021 14:47:53 GMT.
          OssClient::OSS_IF_MODIFIED_SINCE => "Fri, 9 Apr 2021 14:47:53 GMT",
          // Download the object if it was modified at or before Wed, 13 Oct 2021 14:47:53 GMT.
          OssClient::OSS_IF_UNMODIFIED_SINCE => "Fri, 13 Oct 2021 14:47:53 GMT",
          // Download the object if its ETag does not match the specified ETag.
          OssClient::OSS_IF_NONE_MATCH => '"5B3C1A2E0563E1B002CC607C****"',
          // Download the object if its ETag matches the specified ETag.
          OssClient::OSS_IF_MATCH => '"fba9dede5f27731c9771645a3986****"',
        )

    );

    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
    // Download the object to local memory.
    $content = $ossClient->getObject($bucket, $object, $options);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}
print ($content);
print(__FUNCTION__ . ": OK" . "\n");

Conditionally download an object to a local file

The following code shows how to specify conditions to download an 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\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 set. 
$provider = new EnvironmentVariableCredentialsProvider();
// The following example uses the endpoint of the China (Hangzhou) region. Replace the value with the actual endpoint.
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name.
$bucket= "yourBucketName";
// Specify the object name. The object name is the full path of the object that does not include the bucket name, such as exampledir/exampleobject.txt.
$object = "yourObjectName";
// Specify the full path of the local file. For example, D:\\localpath\\examplefile.txt.
$localfile = "yourLocalFile";

try{
    $options = array(
        OssClient::OSS_HEADERS => array(          
           // Download the object if it was modified after Fri, 9 Apr 2021 14:47:53 GMT.
          OssClient::OSS_IF_MODIFIED_SINCE => "Fri, 9 Apr 2021 14:47:53 GMT",
          // Download the object if it was modified at or before Wed, 13 Oct 2021 14:47:53 GMT.
          OssClient::OSS_IF_UNMODIFIED_SINCE => "Fri, 13 Oct 2021 14:47:53 GMT",
          // Download the object if its ETag does not match the specified ETag.
          OssClient::OSS_IF_NONE_MATCH => '"5B3C1A2E0563E1B002CC607C****"',
          // Download the object if its ETag matches the specified ETag.
          OssClient::OSS_IF_MATCH => '"fba9dede5f27731c9771645a3986****"',          
          OssClient::OSS_FILE_DOWNLOAD => $localfile
        )

    );

    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
        "signatureVersion" => OssClient::OSS_SIGNATURE_VERSION_V4,
        "region"=> "cn-hangzhou"
    );
    $ossClient = new OssClient($config);
    $ossClient->getObject($bucket, $object, $options);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}