You can use range download to download parts of the content of an object.

Sample code

The following code provides an example on how to specify a valid range to download data:

// Construct an object download request. 
// Specify the bucket name such as examplebucket and the full path of the object such as exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
GetObjectRequest get = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");

// Specify the download range. 
get.setRange(new Range(0, 99)); // Download the 100 bytes of data within the range of Byte 0 to Byte 99. The valid range starts from Byte 0. 
// get.setRange(new Range(100, Range.INFINITE)); // Download the data within the range of Byte 100 to the end of the object. 

OSSAsyncTask task = oss.asyncGetObject(get, new OSSCompletedCallback<GetObjectRequest, GetObjectResult>() {
    @Override
    public void onSuccess(GetObjectRequest request, GetObjectResult result) {
        // The specified part of the object is downloaded. 
        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) {
        // Handle request exceptions. 
        if (clientExcepion != null) {
            // Handle client exceptions, such as network exceptions. 
            clientExcepion.printStackTrace();
        }
        if (serviceException != null) {
            // Handle service exceptions. 
            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 for range download, visit GitHub.
  • For more information about the API operation that you can call to perform range download, see GetObject.