2012-06-15 97 views
9

是否有直接的方法通過提供路徑來獲取文件ID(例如/some/folder/deep/inside/file.txt)?我知道這可以通過遞歸檢查文件夾的內容來完成,但簡單的調用會更好。獲取給定路徑的文件ID

感謝

回答

3

我們目前沒有這種支持,但反饋肯定會被認爲是我們繼續構建了V2 API。

+0

關於此事的任何消息?看來目前的V2 API還沒有這樣的功能。我會「移植」現有的邏輯,使Box更容易使用! –

+0

借調。每個人都需要通過整個文件夾樹進行遞歸來查找子文件夾ID爲 – Ian1971

+2

,這確實是低效的。我只是試圖將Box API作爲Hazaar MVC的文件系統後端實現,並沿着現有的後端(如Dropbox,GDrive,WebDAV等)以及它不支持的事實讓我的下巴掉落。我正在編寫這個項目的項目可能會說20層深的文件夾,所以我只需要20個請求來獲取單個文件的fileID?開玩笑。我認爲與谷歌驅動器工作是不好的..... –

0

這另一種方法是從路徑中提取目標文件/文件夾的名稱,並使用搜索API

這樣的尋找它:https://api.box.com/2.0/search?query=filename.txt

這還給所有的匹配條目的path_collections爲每個條目提供整個層次結構。事情是這樣的:

"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" 
        } 
       ] 
      } 

路徑此條可以進行反向工程爲/dummy/filename.txt

只是比較反對您正在尋找的路徑此路徑。如果匹配,那麼這就是您要查找的搜索結果。這只是爲了減少您需要進行的ReST電話的數量以獲得結果。希望它是有道理的。

+0

它確實有意義,但只有當您只有唯一的文件名時纔有效,只要您有兩個具有相同名稱的文件,您將無法再應用此邏輯。更不用說,如果有數百萬個文件在整個基地上運行搜索而不限制範圍,這並不是一個真正有效的方法。 –

+0

上面的鏈接不起作用:| – 9codie05

0

這裏是我如何獲得基於路徑的文件夾ID,而無需遞歸遍歷整個樹,這也可以很容易地適應文件。這是基於PHP和CURL,但它也很容易在任何其他應用程序中使用它:

//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";}