gpt4 book ai didi

google-drive-api - 使用 google drive rest 递归搜索文件

转载 作者:行者123 更新时间:2023-12-03 19:49:51 27 4
gpt4 key购买 nike

我正在尝试获取在父目录下创建的所有文件。父目录有很多子目录,然后是这些目录中的文件。

parent
--- sub folder1
--- file1
--- file2

目前,我正在获取子文件夹的所有 id 并构建一个查询,例如 q: 'subfolder1id' in parents 或 'subfolder2id' in parents 以查找文件列表。然后我分批发出这些。如果我有 100 个文件夹,我会针对 10 个批量大小发出 10 个搜索查询。

有没有更好的方法来使用 google drive rest api 查询文件,它可以通过一个查询获得所有文件?

最佳答案

这是一个 answer你的问题。

与您的场景相同的想法:

folderA____ folderA1____folderA1a
\____folderA2____folderA2a
\___folderA2b


我认为您可以从中获得 3 个替代答案。

Alternative 1. Recursion

The temptation would be to list the children of folderA, for any children that are folders, recursively list their children, rinse, repeat. In a very small number of cases, this might be the best approach, but for most, it has the following problems:-

  • It is woefully time consuming to do a server round trip for each sub folder. This does of course depend on the size of your tree, so if you can guarantee that your tree size is small, it could be OK.

Alternative 2. The common parent

This works best if all of the files are being created by your app (ie. you are using drive.file scope). As well as the folder hierarchy above, create a dummy parent folder called say "MyAppCommonParent". As you create each file as a child of its particular Folder, you also make it a child of MyAppCommonParent. This becomes a lot more intuitive if you remember to think of Folders as labels. You can now easily retrieve all descdendants by simply querying MyAppCommonParent
in parents
.

Alternative 3. Folders first

Start by getting all folders. Yep, all of them. Once you have them all in memory, you can crawl through their parents properties and build your tree structure and list of Folder IDs. You can then do a single files.list?q='folderA' in parents or 'folderA1' in parents or
'folderA1a' in parents....
Using this technique you can get everything in two http calls.

Alternative 2 is the most effificient, but only works if you have control of file creation. Alternative 3 is generally more efficient than Alternative 1, but there may be certain small tree sizes where 1 is best.

关于google-drive-api - 使用 google drive rest 递归搜索文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46545336/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com