If you need 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.

Sample code

The following code provides an example on how to use the GetObjectRequest method synchronously to perform a streaming download task:

// 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");

// Configure 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 {
    // Process the download request synchronously and obtain the result. 
    GetObjectResult getResult = oss.getObject(get);

    Log.d("Content-Length", "" + getResult.getContentLength());

    // Obtain 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. For example, display the image or perform a write operation on the object. 
    }

    // View the object metadata. 
    ObjectMetadata metadata = getResult.getMetadata();
    Log.d("ContentType", metadata.getContentType());


} catch (ClientException e) {
    // Handle client 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();
}

The following code provides an example on how to use the GetObjectRequest method asynchronously to perform a streaming download task:

// 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");

// Configure 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 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 streaming download, visit GitHub.
  • For more information about the API operation that you can call to perform streaming download, see GetObject.