If you have a large object that takes a long time to download in its entirety, you can use streaming download to download the object incrementally until the object is downloaded in full.

You can download an object in the streaming download method and then obtain the input stream of the object. You must have read permissions on the object to perform streaming download.

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

// Construct an object download request.
// objectKey is equivalent to objectName and indicates the complete path of the object that you want to download from OSS. The path must include the file extension of the object. For example, you can set objectKey to abc/efg/123.jpg.
GetObjectRequest get = new GetObjectRequest("<bucketName>", "<objectKey>");

// Configure download 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 ex) {
    // 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 call the GetObjectRequest method asynchronously to perform a streaming download task:

GetObjectRequest get = new GetObjectRequest("<bucketName>", "<objectKey>");
// Configure download 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());
        }
    }
});