12

is there a direct method to get file ID by giving a path (e.g. /some/folder/deep/inside/file.txt)? I know this can be done by recursively checking folder's contents, but a simple call would be much better.

Thanks

Zhizhong Liu
  • 141
  • 1
  • 4

3 Answers3

4

We currently don't have support for this, but the feedback will definitely be considered as we continue building out the v2 API.

seanrose
  • 8,185
  • 3
  • 20
  • 21
  • Any news on the matter? It seems the current V2 API hasn't got such functionality yet. I'd make "porting" existing logic to work with Box much easier! – Quint Stoffers Jul 02 '12 at 15:47
  • seconded. It's really inefficient for everybody to have to recurse through the entire folder tree to find a subfolder id – Ian1971 Nov 14 '12 at 12:23
  • 2
    thirded. I'm just trying to implement the Box API as a filesystem backend for Hazaar MVC along side existing backends (like dropbox, GDrive, WebDAV, etc) and the fact that it doesn't support that has just made my jaw drop. A project I'm writing this for can have say 20 levels deep of folders, so I have to do 20 requests just to get the fileID of a single file? f'ing joke. And I thought working with Google Drive was bad..... – Jamie Carl Aug 19 '15 at 03:28
  • 1
    same here. Would highly appreciate if https://api.box.com/2.0/files had a version taking an absolute path. – berbie Apr 05 '17 at 11:28
  • Any news on this? – tommy.carstensen Apr 06 '18 at 11:11
  • 1
    Please add this. Other services like Dropbox and OneDrive have at least some basic way to get a file by path. – worpet Nov 16 '20 at 20:03
  • 1
    It’s the biggest downfall of box api still. – Irish Redneck May 02 '23 at 23:12
0

An alternative to this would be to extract the target file/folder name from the path and search for it using the search API

like this: https://api.box.com/2.0/search?query=filename.txt

This gives back all the matching entries with their path_collections which provides the whole hierarchy for every entry. Something like this:

 "path_collection": {
                "total_count": 2,
                "entries": [
                    {
                        "type": "folder",
                        "id": "0",
                        "sequence_id": null,
                        "etag": null,
                        "name": "All Files"
                    },
                    {
                        "type": "folder",
                        "id": "2988397987",
                        "sequence_id": "0",
                        "etag": "0",
                        "name": "dummy"
                    }
                ]
           }

Path for this entry can be reverse engineered as /dummy/filename.txt

Just compare this path against the path you're looking for. If it matches, then that's the search result you're looking for. This is just to reduce the number of ReST calls you need to make to arrive at the result. Hope it makes sense.

Kryptic Coder
  • 612
  • 2
  • 8
  • 20
  • 1
    It does make sense, but it only work if you only have unique file names, as soon as you have 2 files with the same name you will not be able to apply this logic any more. Not to mention, that in case of millions of files running a search on the entire base without limiting the scope isn't really an efficient approach. – Emil Borconi Oct 04 '15 at 15:18
0

Here is my approach on how to get a folder id based on a path, without recursively going through the whole tree, this can be easily adapted for file as well. This is based on PHP and CURL, but it's very easy to use it in any other application as well:

//WE SET THE SEARCH FOLDER:
$search_folder="XXXX/YYYYY/ZZZZZ/MMMMM/AAAAA/BBBBB";

//WE NEED THE LAST BIT SO WE CAN DO A SEARCH FOR IT
$folder_structure=array_reverse (explode("/",$search_folder));

// We run a CURL (I'm assuming all the authentication and all other CURL parameters are already set!) to search for the last bit, if you want to search for a file rather than a folder, amend the search query accordingly
curl_setopt($curl, CURLOPT_URL, "https://api.box.com/2.0/search?query=".urlencode($folder_structure[0])."&type=folder");    

// Let's make a cine array out of that response
$json=json_decode(curl_exec($curl),true);
$i=0;
$notthis=true;

// We need to loop trough the result, till either we find a matching element, either we are at the end of the array
while ($notthis && $i<count($json['entries'])) {
  $result_info=$json['entries'][$i];

     //The path of each search result is kept in a multidimensional array, so we just rebuild that array, ignoring the first element (that is Always the ROOT)
     if ($search_folder == implode("/",array_slice(array_column($result_info['path_collection']['entries'],'name'),1))."/".$folder_structure[0])
        {
         $notthis=false;
         $folder_id=$result_info['id'];
        }
      else
        {
         $i++;  
        }
   }
if ($notthis) {echo "Path not found....";} else {echo "Folder id: $folder_id";}
Emil Borconi
  • 3,326
  • 2
  • 24
  • 40