5

I'm searching a way to obtain all files of a specific folder.

But I can't find sufficient informations in the official documentation

I think about something like this: (I assume also to set a custom header with the access_token)

https://api.box.com/2.0/search?query=*

This way doesn't work, and I think that the query doen't accept a regex...

Any idea?


PS: A real usecase will help to understand this question:

my folder:

folderOne:
 |
 |_file1.jpg
 |
 |_file2.doc
 |
 |_folder1
 | |_file3.jpg
 | |_folder2
 |
 |_file4.pdf

with the search request I expect to get only file1.jpg, file2.doc and file4.pdf.

RikyTres
  • 676
  • 9
  • 31

2 Answers2

6

You can most easily accomplish this by querying for a folder's contents and then filtering client-side for the file items.

curl https://api.box.com/2.0/folders/FOLDER_ID/items \
-H "Authorization: Bearer ACCESS_TOKEN"

This will return a collection of items, from which you can select those whose type is file.

{
  "total_count": 4,
  "entries": [
     {
        "type": "folder",
        "id": "192429928",
        "name": "folder1"
     },
     {
        "type": "file",
        "id": "818853862",
        "name": "file1.jpg"
     },
     {
        "type": "file",
        "id": "818853843",
        "name": "file2.doc"
     },
     {
        "type": "file",
        "id": "818853832",
        "name": "file4.pdf"
     }
  ]
}

Pagination

Box will return all folder metadata before any file metadata. You can count these folders to determine the appropriate offset for file-only paging. For example, if your Box folder has 13 subfolders, and you want to page 25 files at a time:

/folders/FOLDER_ID/items?limit=25&offset=13
/folders/FOLDER_ID/items?limit=25&offset=38
/folders/FOLDER_ID/items?limit=25&offset=63
/folders/FOLDER_ID/items?limit=25&offset=...
John Hoerr
  • 7,955
  • 2
  • 30
  • 40
  • My goal is pagination... Then if I append at that request limit and offset I get a fail pagination where I could have "empty" pages when an offset have only folder... – RikyTres Jun 09 '14 at 19:43
  • This workaround is good in theory, but is not a acceptable answers to this question... ;) – RikyTres Jun 09 '14 at 19:44
  • 1
    Two things: First, the Search API doesn't do what you want, at least not at the moment. Second, using the method I've described, Box will return metadata for all subfolders, and then all files. You can modify the offset based on the number of folders returned and avoid pagination problems. – John Hoerr Jun 09 '14 at 20:06
  • I done a [first question](http://stackoverflow.com/questions/23953355/retrieve-only-file-from-get-folder-request-to-box-com) where I got that the Search API is the solution. But now if you tell me that, at this moment, I can't do what I want with this API, I can't have a solution only from Box API. Problem is that I can't provide this on my server. By the way, I will attend for an API release where Box improve this kinfd of filtering. – RikyTres Jun 10 '14 at 10:21
  • 1
    I've updated my answer to provide an example of how to accomplish exactly what you're asking for, with paging. Good luck! – John Hoerr Jun 10 '14 at 12:52
0

I have the same problem as the OP an retrieving all files and the only solution I thought at first was by parsing all folders (with sub-folders, sub-sub-folders etc) which is a very tedious task. Another issue with this brute force approach is that Box has a query limit of 1000 API requests per minute per user, so querying the entire tree of folders is not feasible.

Using the Search API is a great idea, however, searching with a blank query or an asterisk won't work using the Box API. Luckily, the files I needed are all in pdf format so rather than searching for the specific filename, I queried for "pdf" instead. This will return to me all the pdfs contained in the user account. I also added type=file to filter out any folders whose folder name might contain the word "pdf". Here's my entire query:

https://api.box.com/2.0/search?query=pdf&type=file&offset=0&limit=100

I added offset and limit parameters for pagination purposes.

Annie Lagang
  • 3,185
  • 1
  • 29
  • 36