Skip to main content

I'm looking for a way to optimize my current Box folder scanning function to reduce the number of API calls made to Box.

Currently, I have a desktop application that scans specific Box folders within my organization. Below is the scanning function in Java:

private static BoxItem getBoxItemRec(BoxFolder initialFolder, Path locations, int index) throws NotFoundException {

    Path currentPath = locations.subpath(0, index + 1);

    String location = locations.getName(index).toString();

    for (BoxItem.Info info : initialFolder.getChildren()) {

        if (info.getName().equalsIgnoreCase(location)) {

            if (locations.getNameCount() == index + 1) return (BoxItem) info.getResource();

            else return getBoxItemRec((BoxFolder) info.getResource(), locations, index + 1);

        }

    }

    throw new NotFoundException(currentPath + " not exist!");

}

 

 

Is there a way to retrieve only the new files that were added within the last X days without scanning the entire Box folder? Ideally, instead of iterating over all the folder files, I would like to generate a list of just the new files from the last X days. The solution doesn't need to be recursive or scan internal folders as my current function does.

Hi @ben halfon 👋

You can definitely optimize your Box folder scanning function and reduce the number of API calls. For that, you can use the Box API's events endpoint to retrieve only the new files that were added within the last X days.

Here's a suggestion on how to modify your code:

  1. Use the `BoxEvent.getEnterpriseEvents()` method to retrieve a list of enterprise events.
  2. Filter the events based on their type and timestamp to get only file creation events within the desired time range.
  3. Extract the file IDs from these events.
  4. Use the `BoxFile.getInfo()` method with each file ID to get detailed information about each new file.

This approach will allow you to retrieve only the new files added within the last X days without scanning the entire Box folder or its internal folders recursively. 😎


I attempted to use the `EnterpriseEventsRequest`, but I'm looking for a way to retrieve the last X `ITEM_UPLOAD` and `ITEM_CREATE` events between two dates for a specific Box folder.
From what I understand, the SDK allows querying for all `ITEM_UPLOAD` and `ITEM_CREATE` events across my entire enterprise within a date range, but it doesn't seem to support filtering for events related to a specific folder. Since my enterprise generates a large number of events unrelated to my application, I only need the events related to file creation within a particular folder.


Hey There - What you are looking to do isn’t currently possible with the Box API. The event endpoint is going to be your best bet. That said - instead of using enterprise - you could use the user events option with a user that owns the folder… and use the “changes” stream type. That way you don’t have to filter through every event from the box instance. 

 

you could also potentially use a V2 webhook


Reply