Updated on 2024-06-21 GMT+08:00

Performing a Conditioned Download

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the .

When downloading an object, you can specify one or more conditions. Only when the conditions are met, the object will be downloaded. Otherwise, an exception will be thrown and the download will fail.

You can set the following conditions:

Parameter

Description

Method in OBS Android SDK

If-Modified-Since

Returns the object if it has been modified since the specified time; otherwise, an error is returned.

GetObjectRequest.setIfModifiedSince

If-Unmodified-Since

Returns the object if it has not been modified since the specified time; otherwise, an error is returned.

GetObjectRequest.setIfUnmodifiedSince

If-Match

Returns the source object if its ETag is the same as the one specified by this parameter; otherwise, an exception is thrown.

GetObjectRequest.setIfMatchTag

If-None-Match

Returns the source object if its ETag is different from the one specified by this parameter; otherwise, an exception is thrown.

GetObjectRequest.setIfNoneMatchTag

  • The ETag of an object is the MD5 check value of the object.
  • If a request includes If-Unmodified-Since or If-Match and the specified condition is not met, 412 Precondition Failed will be returned.
  • If a request includes If-Modified-Since or If-None-Match, and the specified condition is not met, 304 Not Modified will be returned.

Sample code:

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an instance of ObsClient.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

GetObjectRequest request = new GetObjectRequest("bucketname", "objectname");
request.setRangeStart(0l);
request.setRangeEnd(1000l);

request.setIfModifiedSince(new SimpleDateFormat("yyyy-MM-dd").parse("2016-01-01"));
ObsObject obsObject = obsClient.getObject(request);

obsObject.getObjectContent().close();