You can configure one or more conditions for downloads. If the specified conditions are met, the object can be downloaded. If the specified conditions are not met, an error code is returned and the object cannot be downloaded. You can configure the following conditions:
|If-Modified-Since||If the specified time is earlier than the time the object is modified, the object can be downloaded. Otherwise, an error (304 Not modified) is returned.|
|If-Unmodified-Since||If the specified time is later than or equal to the time the object is modified, the object can be downloaded. Otherwise, an error (412 Precondition failed) is returned.|
|If-Match||If the specified ETag matches that of an object, the object can be downloaded. Otherwise, an error (412 Precondition failed) is returned.|
|If-None-Match||If the specified ETag does not match that of an object, the object can be downloaded. Otherwise, an error (304 Not modified) is returned.|
Both If-Modified-Since and If-Unmodified-Since can exist at the same time as object download conditions. Both If-Match and If-None-Match can exist at the same time as object download conditions.
ossClient.getObjectMeta can be used to obtain the ETag.
Run the following code for conditional download:
//This example uses the endpoint China East 1 (Hangzhou). Specify the actual endpoint based on your requirements. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com"; // It is highly risky to log on with AccessKey of an Alibaba Cloud account because the account has permissions on all the APIs in OSS. We recommend that you log on as a RAM user to access APIs or perform routine operations and maintenance. To create a RAM account, log on to https://ram.console.aliyun.com. String accessKeyId = "<yourAccessKeyId>"; String accessKeySecret = "<yourAccessKeySecret>"; String bucketName = "<yourBucketName>"; String objectName = "<yourObjectName>"; // Create an OSSClient instance. OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); GetObjectRequest request = new GetObjectRequest(bucketName, objectName); // Configure conditions. request.setModifiedSinceConstraint(new Date()); // Download an object to a local file. ossClient.getObject(request, new File("<yourLocalFile>")); //Close your OSSClient. ossClient.shutdown();
Conditional download supports ossClient.getObject and ossClient.downloadFile.