You can specify one or more conditions when you download an object (file). The object is downloaded only if the specified conditions are met. If the conditions are not met, the download fails and an error is returned.
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 Initialization.
To perform conditional download, you must have the
oss:GetObjectpermission. For more information, see Attach a custom policy to a RAM user.
Sample code
The following code shows how to perform a conditional download:
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the endpoint is located, for example, cn-hangzhou. Note that this parameter is required for V4 signatures.
region = "cn-hangzhou"
# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. Do not include the bucket name. For example, exampledir/exampleobject.txt. For more information about object naming conventions, see Object.
object_name = 'exampledir/exampleobject.txt'
headers = dict()
# If the specified time is earlier than the actual modification time of the object, the object is downloaded. Otherwise, a 304 Not Modified error is returned.
headers['If-Modified-Since'] = 'Mon, 13 Dec 2021 14:47:53 GMT'
# If the specified time is the same as or later than the actual modification time of the object, the object is downloaded. Otherwise, a 412 Precondition Failed error is returned.
# headers['If-Unmodified-Since'] = 'Mon, 13 Dec 2021 14:47:53 GMT'
# If the specified ETag matches the ETag of the object, the object is downloaded. Otherwise, a 412 Precondition Failed error is returned.
# headers['If-Match'] = 'DC21493F505BA3739562D8CC452C****'
# If the specified ETag does not match the ETag of the object, the object is downloaded. Otherwise, a 304 Not Modified error is returned.
# headers['If-None-Match'] = 'DC21493F505BA3739562D8CC452C****'
object_stream = bucket.get_object(object_name, headers=headers)
print(object_stream.read())References
For more information about the API operation for conditional downloads, see GetObject.