Updated on 2025-05-16 GMT+08:00

Performing a Partial 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 API Reference.

When only partial data of an object is required, you can download data falling within a specific range. If the specified range is from 0 to 1,000, data from byte 0 to byte 1,000, 1,001 bytes in total, are returned. If the specified range is invalid, data of the whole object will be returned. Sample code is as follows:

// 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/intl/en-us/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 ObsClient instance.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

GetObjectRequest request = new GetObjectRequest("bucketname", "objectname");
// Set the start position and end position for downloading.
request.setRangeStart(0l);
request.setRangeEnd(1000l);
ObsObject obsObject = obsClient.getObject(request);

// Obtain data.
byte[] buf = new byte[1024];
InputStream in = obsObject.getObjectContent();
for (int n = 0; n != -1; ) {
    n = in.read(buf, 0, buf.length);
}

in.close();
  • If the specified range is invalid (because the start or end position is set to a negative integer or the range is larger than the object length), data of the whole object will be returned.
  • Due to HTTP coding restrictions, non-ASCII characters cannot be sent. The SDK will decode the information in response headers using URL decoding rules.
  • If you do not need the SDK to decode for you, call GetObjectRequest.setIsEncodeHeaders(false) to disable auto decoding.
  • You can also call obsObject.getMetadata().getOriginalHeaders() to obtain information about all original response headers.
  • This download method also can be used to concurrently download parts of a large object. For details about the sample code, see ConcurrentDownloadObjectSample.