Able to fetch Box folder items but could not download the file from box using API

I am trying to download a file from Box by passing the file id but getting below error. Although I am able to fetch the folder items from Box and able to successfully get all the file names inside the folder, which suggests that there is a network connectivity between my application and Box api. Also same code is working as expected in local machine but not working when the code is migrated to cloud.

Does download API uses a different port than fetching the folder items? The Box API docs tells that all the API uses https.

INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 | com.box.sdk.BoxAPIException: Couldn't connect to the Box API due to a network error.
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 |         at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:551)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 |         at com.box.sdk.BoxAPIRequest.handleRedirect(BoxAPIRequest.java:615)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.626 |         at com.box.sdk.BoxAPIRequest.trySend(BoxAPIRequest.java:571)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:354)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxAPIRequest.send(BoxAPIRequest.java:329)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxFile.download(BoxFile.java:295)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.box.sdk.BoxFile.download(BoxFile.java:283)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.627 |         at com.ge.hc.integration.service.impl.BoxIntegrationSerImpl.getBoxFileByFileID(BoxIntegrationSerImpl.java:188)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 | Caused by: java.net.ConnectException: Connection timed out (Connection timed out) (local port 38216 to address 0.0.0.0, remote port 443 to address 107.152.27.200)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.PlainSocketImpl.socketConnect(Native Method)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at java.net.Socket.connect(Socket.java:857)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:666)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.net.NetworkClient.doConnect(NetworkClient.java:175)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.net.www.http.HttpClient.openServer(HttpClient.java:474)
INFO   | jvm 1    | main    | 2019/08/08 11:26:32.727 |         at sun.net.www.http.HttpClient.openServer(HttpClient.java:569)`

Code snippet for file download(the one which is throwing error at boxFile.download(stream);) —

String fileDownloadLocation = siteConfigService.getProperty(FILEDOWNLOADLOCATION);
File file = null;
BoxAPIConnection boxConnection = new BoxAPIConnection(accessToken);

    FileOutputStream stream;
    try 
    {
        BoxFile boxFile = new BoxFile(boxConnection, fileID);
        BoxFile.Info info = boxFile.getInfo();
        LOG.info("downloading file to -"+fileDownloadLocation+info.getName());
        stream = new FileOutputStream(fileDownloadLocation+info.getName());
        //LOG.info(stream);
        boxFile.download(stream);
        stream.close();
        file = new File(fileDownloadLocation+info.getName());
    } 

Code snippet to get folder items(the one which is working and printing file names)–

public List<Info> getFolderItems(String accessToken, String folderID) {
    List<Info> fileList = new ArrayList<Info>();
    BoxAPIConnection boxConnection = new BoxAPIConnection(accessToken);
    BoxFolder folder = new BoxFolder(boxConnection, folderID);

    for (BoxItem.Info itemInfo : folder) {
        if (itemInfo instanceof BoxFile.Info) {
            BoxFile.Info fileInfo = (BoxFile.Info) itemInfo;
            fileList.add(fileInfo);
            LOG.info("***FILE NAME-"+fileInfo.getName());
        } else if (itemInfo instanceof BoxFolder.Info) {
            BoxFolder.Info folderInfo = (BoxFolder.Info) itemInfo;
        }
    }
    return fileList;
}

Answer

Box hosts file content on a different domain (dl.boxcloud.com) than the API (api.box.com). When you download a file via the API, the API returns a temporary URL on the boxcloud.com domain. The Java SDK automatically follows this redirect to fetch the file content. All requests are issued over port 443 (HTTPS).

This might just be a transient error. If it persists you might check to see whether your cloud service has any firewall rules configured. Here are the domains and IP addresses involved in this operation:

api.box.com: 107.152.26.197, 107.152.27.197

dl.boxcloud.com: 107.152.26.200, 107.152.27.200

Note: the dl.boxcloud.com IP appears in your error logs, which suggests that the SDK is following the redirect as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *