When you download an object from a bucket, you can specify conditions based on the object's last modified time or its ETag (a unique identifier for the object's content). The object is downloaded only if the specified conditions are met. If the conditions are not met, an error is returned. Conditional downloads reduce network traffic and resource consumption, which improves download efficiency.
Conditions
OSS supports the following conditions:
If-Modified-Since and If-Unmodified-Since can be specified in the same request. If-Match and If-None-Match can also be specified in the same request.
You can call the ossClient.getObjectMeta method to obtain the ETag.
Parameter | Description |
If-Modified-Since | If the specified time is earlier than the actual modification time, the file is transferred. Otherwise, an error (304 Not modified) is returned. |
If-Unmodified-Since | If the specified time is the same as or later than the actual modification time of the file, the file is transferred. Otherwise, an error (412 Precondition failed) is returned. |
If-Match | If the specified ETag matches the ETag of the OSS object, the object is transferred. Otherwise, an error (412 Precondition failed) is returned. |
If-None-Match | If the specified ETag does not match the ETag of the OSS object, the object is transferred. Otherwise, an error (304 Not modified) is returned. |
Usage notes
In this topic, the public endpoint of the China (Hangzhou) region is used. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint. For details about supported regions and endpoints, see Regions and endpoints.
In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
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 Configuration examples for common scenarios.
To perform a conditional download, you must have the
oss:GetObject
permission. For more information, see Attach a custom policy to a RAM user.
Examples
The following sample code shows how to use the ossClient.getObject method to perform a conditional download. You can also use the ossClient.downloadFile method to perform a conditional download in a similar way.
import com.aliyun.oss.*; import com.aliyun.oss.common.auth.*; import com.aliyun.oss.common.comm.SignVersion; import com.aliyun.oss.model.GetObjectRequest; import java.io.File; import java.util.Date; public class Demo { public static void main(String[] args) throws Exception { // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. String endpoint = "https://oss-cn-hangzhou.aliyuncs.com"; // 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 configured. EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider(); // Specify the bucket name, for example, examplebucket. String bucketName = "examplebucket"; // Specify the full path of the object. Do not include the bucket name in the full path. Example: testfolder/exampleobject.txt. String objectName = "testfolder/exampleobject.txt"; String pathName = "D:\\localpath\\examplefile.txt"; // Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. String region = "cn-hangzhou"; // Create an OSSClient instance. // When the OSSClient instance is no longer in use, call the shutdown method to release resources. ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); OSS ossClient = OSSClientBuilder.create() .endpoint(endpoint) .credentialsProvider(credentialsProvider) .clientConfiguration(clientBuilderConfiguration) .region(region) .build(); try { GetObjectRequest request = new GetObjectRequest(bucketName, objectName); // Assume that the object was last modified at 13:27:04 on September 26, 2023. If you specify a Date object that is earlier than this time, such as Tue Sep 25 13:27:04 CST 2023, the If-Modified-Since condition is met and the download is triggered. request.setModifiedSinceConstraint(new Date("Tue Sep 25 13:27:04 CST 2023")); // Download the OSS object to a local file. ossClient.getObject(request, new File(pathName)); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message:" + oe.getErrorMessage()); System.out.println("Error Code:" + oe.getErrorCode()); System.out.println("Request ID:" + oe.getRequestId()); System.out.println("Host ID:" + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message:" + ce.getMessage()); } finally { if (ossClient != null) { ossClient.shutdown(); } } } }
References
For more information about the API operation for conditional downloads, see GetObject.