You can specify one or more conditions when you download objects. If the specified conditions are met, the object is downloaded. If the specified conditions are not met, an error is returned and the object is not downloaded. You can use conditional download to download an object from Object Storage Service (OSS) to your local memory or a local file.

Conditions

The following table describes conditions that OSS supports and you can specify when you use conditional download. You can use If-Modified-Since and If-Unmodified-Since as object download conditions at the same time. You can use If-Match and If-None-Match as object download conditions at the same time.

Parameter Description Method
If-Modified-Since If the object is modified after the specified time, the object can be downloaded. Otherwise, the error code 304 Not Modified is returned. OssClient::OSS_IF_MODIFIED_SINCE
If-Unmodified-Since If the object is modified before or at the specified time, the object can be downloaded. Otherwise, the error code 412 Precondition Failed is returned. OssClient::OSS_IF_UNMODIFIED_SINCE
If-Match If the specified ETag matches that of an object, the object can be downloaded. Otherwise, the error 412 Precondition Failed is returned.
Note You can use $ossClient->getObjectMeta to obtain the ETag of an object.
OssClient::OSS_IF_MATCH
If-None-Match If the specified ETag does not match that of an object, the object can be downloaded. Otherwise, the error code 304 Not Modified is returned. OssClient::OSS_IF_NONE_MATCH

Download an object to your local memory by using conditional download

The following code provides an example on how to specify conditions and download the object that matches the conditions to your 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\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 Resource Access Management (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>";
// The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint. 
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. 
$bucket= "<yourBucketName>";
// Specify the object name. // Specify the full path of the object. The full path of the object does not contain the bucket name. Example: exampledir/exampleobject.txt. 
$object = "<yourObjectName>";

try{
    $options = array(
        OssClient::OSS_HEADERS => array(          
          // Set If-Modified-Since to 14:47:53 Friday, April 9, 2021 in GMT. 
          OssClient::OSS_IF_MODIFIED_SINCE => "Fri, 9 Apr 2021 14:47:53 GMT",
          // Set If-Unmodified-Since to 14:47:53 Wednesday, October 13, 2021 in GMT. 
          OssClient::OSS_IF_UNMODIFIED_SINCE => "Fri, 13 Oct 2021 14:47:53 GMT",
          // Set If-None-Match to the ETag of the object. 
          OssClient::OSS_IF_NONE_MATCH => '"5B3C1A2E0563E1B002CC607C****"',
          // Set If-Match to the ETag of the object. 
          OssClient::OSS_IF_MATCH => '"fba9dede5f27731c9771645a3986****"',
        )

    );

    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    // Download the object to your 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");

Download an object to a local file by using conditional download

The following code provides an example on how to specify conditions and download the object that matches the conditions 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\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>";
// The China (Hangzhou) region is used in this example as the endpoint. Specify the actual endpoint. 
$endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
// Specify the bucket name. 
$bucket= "<yourBucketName>";
// Specify the object name. // Specify the full path of the object. The full path of the object does not contain the bucket name. Example: exampledir/exampleobject.txt. 
$object = "<yourObjectName>";
// Specify the full path of the local file. Example: D:\\localpath\\examplefile.txt. 
$localfile = "<yourLocalFile>";

try{
    $options = array(
        OssClient::OSS_HEADERS => array(          
           // Set If-Modified-Since to 14:47:53 Friday, April 9, 2021 in GMT. 
          OssClient::OSS_IF_MODIFIED_SINCE => "Fri, 9 Apr 2021 14:47:53 GMT",
          // Set If-Unmodified-Since to 14:47:53 Wednesday, October 13, 2021 in GMT. 
          OssClient::OSS_IF_UNMODIFIED_SINCE => "Fri, 13 Oct 2021 14:47:53 GMT",
          // Set If-None-Match to the ETag of the object. 
          OssClient::OSS_IF_NONE_MATCH => '"5B3C1A2E0563E1B002CC607C****"',
          // Set If-Match to the ETag of the object. 
          OssClient::OSS_IF_MATCH => '"fba9dede5f27731c9771645a3986****"',          
          OssClient::OSS_FILE_DOWNLOAD => $localfile
        )

    );

    $ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
    $ossClient->getObject($bucket, $object, $options);
} catch(OssException $e) {
    printf(__FUNCTION__ . ": FAILED\n");
    printf($e->getMessage() . "\n");
    return;
}