Conditional download using OSS SDK for Python 1.0
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.
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.
-
This topic demonstrates creating an OSSClient instance with an OSS endpoint. For alternative configurations, such as using a custom domain or authenticating with credentials from Security Token Service (STS), see Initialization.
-
Conditional download requires the
oss:GetObjectpermission. For more information, see Grant a custom policy.
Examples
The following sample code provides an example on how to perform conditional download:
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# 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.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the ID of the region that maps to the endpoint. Example: cn-hangzhou. This parameter is required if you use the signature algorithm V4.
region = "cn-hangzhou"
# Specify the name of your bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)
# Specify the full path of the object. Do not include the bucket name in the full path. Example: exampledir/exampleobject.txt. For more information about the naming conventions for objects, see Object naming conventions.
object_name = 'exampledir/exampleobject.txt'
headers = dict()
# If the time that is specified in the request is earlier than the actual modified time of the object, the object is downloaded. Otherwise, the error code "304 Not modified" is returned.
headers['If-Modified-Since'] = 'Mon, 13 Dec 2021 14:47:53 GMT'
# If the time that is specified in the request is the same as or later than the actual modified time of the object, the object is downloaded. Otherwise, the error code 412 Precondition failed is returned.
# headers['If-Unmodified-Since'] = 'Mon, 13 Dec 2021 14:47:53 GMT'
# If the ETag value that is specified in the request matches the ETag value of the object, the object is downloaded. Otherwise, the error code "412 Precondition failed" is returned.
# headers['If-Match'] = 'DC21493F505BA3739562D8CC452C****'
# If the ETag value that is specified in the request does not match the ETag value of the object, the object is downloaded. Otherwise, the error code "304 Not modified" 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 that you can call to perform conditional download, see GetObject.