是否有通过提供路径获取文件ID的直接方法(例如/some/folder/deep/inside/file.txt)?我知道这可以通过递归检查文件夹的内容来完成,但一个简单的调用会好得多。
由于
答案 0 :(得分:4)
我们目前对此没有支持,但在我们继续构建v2 API时,肯定会考虑反馈。
答案 1 :(得分: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调用次数。希望它有意义。
答案 2 :(得分: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";}