All Products
Search
Document Center

Object Storage Service:Conditional download (Android SDK)

Last Updated:Sep 21, 2026

You can specify one or more conditions when you download an object. If the specified conditions are met, the object is downloaded. Otherwise, the object is not downloaded and an error is returned.

Usage notes

  • Before you run the sample code in this topic, you must create an OSSClient instance by using methods such as using a custom domain name or Security Token Service (STS). For more information, see Initialization (Android SDK).

Download conditions

OSS supports the following download conditions.

Note
  • If-Modified-Since and If-Unmodified-Since can be used together. If-Match and If-None-Match can also be used together.

  • Obtain the ETag by calling ossClient.getObjectMeta.

Parameter

Description

If-Modified-Since

If the specified time is earlier than the object's last modified time, the object is downloaded. Otherwise, 304 Not Modified is returned.

If-Unmodified-Since

If the specified time is equal to or later than the object's last modified time, the object is downloaded. Otherwise, 412 Precondition Failed is returned.

If-Match

If the specified ETag matches the object's ETag, the object is downloaded. Otherwise, 412 Precondition Failed is returned.

If-None-Match

If the specified ETag does not match the object's ETag, the object is downloaded. Otherwise, 304 Not Modified is returned.

Examples

The following sample code provides an example on how to perform conditional download:

// Set the bucket name, such as examplebucket, and the full path of the object, such as exampledir/exampleobject.txt.
// The full path of the object cannot contain the bucket name.
String bucketName = "examplebucket";
String objectKey = "exampledir/exampleobject.txt";
// Create a request to download the object.
Map<String, String> headers = new HashMap<>();
// If the specified time is earlier than the object's last modification time, the object is downloaded. Otherwise, a 304 Not modified error is returned.
headers.put(OSSHeaders.GET_OBJECT_IF_MODIFIED_SINCE, "Fri, 13 Nov 2015 14:47:53 GMT");
// If the specified time is the same as or later than the object's last modification time, the object is downloaded. Otherwise, a 412 Precondition failed error is returned.
// headers.put(OSSHeaders.GET_OBJECT_IF_UNMODIFIED_SINCE, "Fri, 13 Nov 2015 14:47:53 GMT");
// If the specified ETag matches the object's ETag, the object is downloaded. Otherwise, a 412 Precondition failed error is returned.
// headers.put(OSSHeaders.GET_OBJECT_IF_MATCH, "5B3C1A2E0563E1B002CC607C*****");
// If the specified ETag does not match the object's ETag, the object is downloaded. Otherwise, a 304 Not modified error is returned.
// headers.put(OSSHeaders.GET_OBJECT_IF_NONE_MATCH, "5B3C1A2E0563E1B002CC607C*****");
GetObjectRequest get = new GetObjectRequest(bucketName, objectKey);
get.setRequestHeaders(headers);

OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
    @Override
    public void onSuccess(GetObjectRequest request, GetObjectResult result) {
        // The request is successful.
        InputStream inputStream = result.getObjectContent();
        byte[] buffer = new byte[2048];
        int len;
        try {
            while ((len = inputStream.read(buffer)) != -1) {
                // Process the downloaded data.
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onFailure(GetObjectRequest request, ClientException clientExcepion, ServiceException serviceException) {
        // The request failed.
        if (clientExcepion != null) {
            // A client exception occurred, such as a network exception.
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // A service exception occurred.
            Log.e("ErrorCode", serviceException.getErrorCode());
            Log.e("RequestId", serviceException.getRequestId());
            Log.e("HostId", serviceException.getHostId());
            Log.e("RawMessage", serviceException.getRawMessage());
        }
    }
});

References

  • For the complete sample code that is used to perform conditional download, visit GitHub.

  • For more information about the API operation that you can call to perform conditional download, see GetObject.

  • For more information about how to initialize an OSSClient instance, see Initialization.