This topic describes how to download objects by setting conditions.

You can specify one or more conditions when downloading objects to determine that the object can be downloaded if the conditions are met, and an error code is returned and the object cannot be downloaded if the conditions are not met. The following table describes the conditions that you can specify.

Parameter Description
If-Modified-Since Indicates that if the specified time is earlier than the time when the object is modified, the object can be downloaded. Otherwise, a 304 Not Modified error is returned.
If-Unmodified-Since Indicates that if the specified time is later than or equal to the time when the object is modified, the object can be downloaded. Otherwise, a 412 Precondition Failed error is returned.
If-Match Indicates that if the specified ETag matches that of an object, the object can be downloaded. Otherwise, a 412 Precondition Failed error is returned.
If-None-Match Indicates that if the specified ETag does not match that of an object, the object can be downloaded. Otherwise, a 304 Not Modified error is returned.

If-Modified-Since and If-Unmodified-Since can be configured as download conditions at the same time. If-Match and If-None-Match can be configured as download conditions at the same time.

You can use ossClient.getObjectMeta to obtain the ETag of an object.

Run the following code to download an object with conditions:
public void testGetObjectWithHeaders() throws Exception {
        GetObjectRequest request = new GetObjectRequest(bucketName, objectName);
        request.setRequestHeaders(...);
        

        OSSAsyncTask task = oss.asyncGetObject(request, getCallback);
        task.waitUntilFinished();

        GetObjectRequest rq = getCallback.request;
        GetObjectResult result = getCallback.result;
        
    }