To download a large object or if the download requires a long period of time to complete, you can perform streaming download to download the object in increments.
You can download an object by performing streaming download and obtain the input stream of the object. You must have the read permission on the object to perform streaming download.
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
Synchronous call:
// Create a request to download an object.
// Specify the bucket name, for example, examplebucket, and the full path of the object, for example, exampledir/exampleobject.txt. The full path of the object cannot contain the bucket name.
GetObjectRequest get = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");
// Set the download progress callback.
get.setProgressListener(new OSSProgressCallback<GetObjectRequest>() {
@Override
public void onProgress(GetObjectRequest request, long currentSize, long totalSize) {
OSSLog.logDebug("getobj_progress: " + currentSize+" total_size: " + totalSize, false);
}
});
try {
// Synchronously execute the download request and return the result.
GetObjectResult getResult = oss.getObject(get);
Log.d("Content-Length", "" + getResult.getContentLength());
// Get the input stream of the object.
InputStream inputStream = getResult.getObjectContent();
byte[] buffer = new byte[2048];
int len;
while ((len = inputStream.read(buffer)) != -1) {
// Process the downloaded data, such as displaying an image or writing the data to a file.
}
// After the download, you can view the object metadata.
ObjectMetadata metadata = getResult.getMetadata();
Log.d("ContentType", metadata.getContentType());
} catch (ClientException e) {
// Handle local exceptions, such as network exceptions.
e.printStackTrace();
} catch (ServiceException e) {
// Handle service exceptions.
Log.e("RequestId", e.getRequestId());
Log.e("ErrorCode", e.getErrorCode());
Log.e("HostId", e.getHostId());
Log.e("RawMessage", e.getRawMessage());
} catch (IOException e) {
e.printStackTrace();
}Asynchronous invocation:
// Create a request to download an object.
// Specify the bucket name, for example, examplebucket, and the object's full path, for example, exampledir/exampleobject.txt.
// The object's full path cannot contain the bucket name.
GetObjectRequest get = new GetObjectRequest("examplebucket", "exampledir/exampleobject.txt");
// Set the download progress callback.
get.setProgressListener(new OSSProgressCallback<GetObjectRequest>() {
@Override
public void onProgress(GetObjectRequest request, long currentSize, long totalSize) {
OSSLog.logDebug("getobj_progress: " + currentSize+" total_size: " + totalSize, false);
}
});
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 local 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 streaming download, visit GitHub.
For more information about the API operation that you can call to perform streaming download, see GetObject.
For more information about how to initialize an OSSClient instance, see Initialization.