When a client downloads resources over a network, the download is interrupted for reasons such as network fluctuations. In this case, you can perform resumable download to continue downloading the uncompleted parts. This method saves time and traffic.
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.
Examples
The following sample code provides an example on how to perform resumable download:
// Specify the bucket name. For example, examplebucket.
// Specify the full path of the object. Do not include the bucket name. For example, exampledir/exampleobject.txt.
String objectKey = "exampledir/exampleobject.txt";
// Specify the full path of the local file to which the object is downloaded.
String localFile = "/storage/emulated/0/oss/examplefile.txt";
ResumableDownloadRequest request = new ResumableDownloadRequest(bucketName, objectKey, localFile);
// Set the part size in bytes. The default part size is 256 KB (256 * 1024 bytes). You do not need to set this parameter if you have no special requirements.
request.setPartSize(256*1024);
// Enable resumable download.
request.setEnableCheckPoint(true);
// Set the full path of the breakpoint record file. This parameter is required to resume a download only if a breakpoint record file was generated from a previous interruption.
request.setCheckPointFilePath("/storage/emulated/0/oss");
request.setProgressListener(new OSSProgressCallback() {
@Override
public void onProgress(Object request, long currentSize, long totalSize) {
Log.d("ResumableDownload", "currentSize: " + currentSize + " totalSize: " + totalSize);
}
});
OSSAsyncTask task = oss.asyncResumableDownload(request, new OSSCompletedCallback<ResumableDownloadRequest, ResumableDownloadResult>() {
@Override
public void onSuccess(ResumableDownloadRequest request, ResumableDownloadResult result) {
Log.d("ResumableDownload", "DownloadSuccess");
}
@Override
public void onFailure(ResumableDownloadRequest request, ClientException clientException, ServiceException serviceException) {
// Request exception.
if (clientException != null) {
// A client-side exception occurred, such as a network error.
clientException.printStackTrace();
}
if (serviceException != null) {
// A server-side 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 resumable download, visit GitHub.
For more information about the API operation that you can call to perform resumable download, see GetObject.
For more information about how to initialize an OSSClient instance, see Initialization.