All Products
Search
Document Center

Object Storage Service:Conditional download in PHP

Last Updated:Mar 11, 2024

You can specify one or more conditions to 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.

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 perform 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 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\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();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your 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. The path of the object cannot 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",
          // Specify that objects whose ETag values do not match the ETag values that are specified in the requests are downloaded. 
          OssClient::OSS_IF_NONE_MATCH => '"5B3C1A2E0563E1B002CC607C****"',
          // Specify that objects whose ETag values match the ETag values that are specified in the requests are downloaded. 
          OssClient::OSS_IF_MATCH => '"fba9dede5f27731c9771645a3986****"',
        )

    );

    $config = array(
        "provider" => $provider,
        "endpoint" => $endpoint,
    );
    $ossClient = new OssClient($config);
    // 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\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();
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your 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. The path of the object cannot contain the bucket name. Example: exampledir/exampleobject.txt. 
$object = "yourObjectName";
// Specify the full path of the local object that you want to upload. 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",
          // Specify that objects whose ETag values do not match the ETag values that are specified in the requests are downloaded. 
          OssClient::OSS_IF_NONE_MATCH => '"5B3C1A2E0563E1B002CC607C****"',
          // Specify that objects whose ETag values match the ETag values that are specified in the requests are downloaded. 
          OssClient::OSS_IF_MATCH => '"fba9dede5f27731c9771645a3986****"',          
          OssClient::OSS_FILE_DOWNLOAD => $localfile
        )

    );

    $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");
    return;
}