Frage

Is there a way to fetch the complete folder path of a file given the file Id using the Box API?

From the API documentation it is clear that we can use the Parent Folder ID to recursively fetch folder names until the root is reached, however this would result in a lot of REST API calls depending on the number of parent folders.

War es hilfreich?

Lösung

The path_collection property of the file object contains "the path of folders to this item, starting at the root." This information can be fetched in a single API request using only the file ID. See the documentation for a detailed example, a portion of which is shown below. The total_count field indicates the depth of the tree for that file, and the entries field contains information about each folder in the tree. It's my understanding that the entries are returned in order.

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

Andere Tipps

   private String getFullPath(BoxFile.Info info)
   {
      List<BoxFolder> filePathList = info.getPathCollection();
      StringBuffer fullPathStringBuffer = new StringBuffer("");
      for (BoxFolder pf : filePathList)
      {
         fullPathStringBuffer.append("/").append(pf.getInfo().getName());
      }
      System.out.println(fullPathStringBuffer.toString());
      return fullPathStringBuffer.toString();
   }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top