When you download an object from a bucket, you can specify conditions based on the object's last modified time or ETag. The ETag is an 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 and the download is not triggered. Conditional downloads reduce network traffic and resource consumption, and improve download efficiency.
Conditions
OSS supports the following conditions:
If-Modified-Since and If-Unmodified-Since can be used together. If-Match and If-None-Match can also be used together.
To retrieve the ETag, use the ossClient.getObjectMeta method.
Parameter | Description |
If-Modified-Since | If the specified time is earlier than the actual modification time, the file is transferred. Otherwise, a 304 Not Modified error is returned. |
If-Unmodified-Since | If the specified time is the same as or later than the actual modification time, the file is transferred. Otherwise, a 412 Precondition Failed error is returned. |
If-Match | If the specified ETag matches the ETag of the OSS object, the file is transferred. Otherwise, a 412 Precondition Failed error is returned. |
If-None-Match | If the specified ETag does not match the ETag of the OSS object, the file is transferred. Otherwise, a 304 Not Modified error 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 OSS 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:GetObjectpermission. For more information, see Grant custom permissions to a RAM user.
Sample code
The following sample code shows how to use the ossClient.getObject method to perform a conditional download. The ossClient.downloadFile method also supports conditional downloads and is used 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 {
// The endpoint of the China (Hangzhou) region is used as an example. Specify the 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. Example: examplebucket.
String bucketName = "examplebucket";
// Specify the full path of the object. Do not include the bucket name. Example: testfolder/exampleobject.txt.
String objectName = "testfolder/exampleobject.txt";
String pathName = "D:\\localpath\\examplefile.txt";
// Specify the region where the bucket is located. For example, if the bucket is 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 used, 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 an earlier 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.