You can perform range download to download a specific range of data of an object.
Examples
The following sample code provides an example on how to perform range download to download a specific range of data of an object:
// Construct an object download request.
// Specify the name of the bucket and the full path of the object. In this example, the bucket name is examplebucket and the full path of the object is exampledir/exampleobject.txt. Do not include the bucket name in the full path of the object.
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 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) {
// 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 that is used to perform range download, visit GitHub.
For more information about the API operation that you can call to perform range download, see GetObject.
For more information about how to initialize an OSSClient instance, see Initialization.