Conditional download lets you attach one or more conditions to a download request. OSS transfers the object only when the specified conditions are met. If any condition fails, OSS returns an error and skips the transfer.
Both download destinations—local memory and local file—support the same set of conditions.
Prerequisites
Before you begin, make sure that you have:
The
oss:GetObjectpermission. For details, see Attach a custom policy to a RAM user.An
OssClientinstance. To create one using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from another Alibaba Cloud service in the same region, use the internal endpoint instead. For supported regions and endpoints, see Regions and endpoints.
Supported conditions
OSS evaluates the following HTTP conditional headers:
| Parameter | Condition | Error if not met | PHP SDK constant |
|---|---|---|---|
| If-Modified-Since | Object was modified after the specified time | 304 Not Modified | OssClient::OSS_IF_MODIFIED_SINCE |
| If-Unmodified-Since | Object was modified at or before the specified time | 412 Precondition Failed | OssClient::OSS_IF_UNMODIFIED_SINCE |
| If-Match | Object ETag matches the specified value | 412 Precondition Failed | OssClient::OSS_IF_MATCH |
| If-None-Match | Object ETag does not match the specified value | 304 Not Modified | OssClient::OSS_IF_NONE_MATCH |
Combining conditions: If-Modified-Since and If-Unmodified-Since can be used together, as can If-Match and If-None-Match.
Getting an object's ETag: Call $ossClient->getObjectMeta() to retrieve the ETag before using If-Match or If-None-Match. Pass the ETag value as a quoted string—for example, '"fba9dede5f27731c9771645a3986****"'.
Download to local memory
All examples pass conditions inside the OssClient::OSS_HEADERS array of the $options parameter.
<?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");Download to a local file
To save the object to a local file instead of memory, add OssClient::OSS_FILE_DOWNLOAD to the headers array and set it to the destination file path.
<?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;
}