使用 google drive rest 递归搜索文件 [英] Search files recursively using google drive rest

查看:32
本文介绍了使用 google drive rest 递归搜索文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

父级--- 子文件夹1--- 文件 1--- 文件2

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

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

解决方案

这里有一个 回答您的问题.

您的场景中的相同想法:

<块引用>

folderA____ folderA1____folderA1a\____文件夹A2____文件夹A2a\___文件夹A2b

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

<块引用>

替代方案 1. 递归

对于任何作为文件夹的孩子,递归列出他们的孩子,冲洗,重复.在极少数情况下,这可能是最好的方法,但对于大多数人来说,它有以下问题:-

  • 为每个子文件夹执行一次服务器往返非常耗时.这当然取决于你的树的大小,所以如果你可以保证你的树很小,应该没问题.

备选方案 2. 共同父母

如果所有文件都是由您的应用程序创建的(即.您正在使用 drive.file 范围).以及文件夹层次结构在上面,创建一个名为MyAppCommonParent"的虚拟父文件夹.作为您将每个文件创建为其特定文件夹的子文件,您还使其成为 MyAppCommonParent 的子项.这变得更多如果您记得将文件夹视为标签,那么直观.您现在可以只需查询 MyAppCommonParent 即可轻松检索所有后代在父母中.

备选方案 3. 文件夹优先

首先获取所有文件夹.是的,所有这些.一旦你拥有了它们在内存中,您可以爬取其父属性并构建您的树结构和文件夹 ID 列表.然后你可以做一个files.list?q='folderA' in parents or 'folderA1' in parents or'folderA1a' in parents.... 使用这种技术你可以得到两个 http 调用中的所有内容.

备选方案 2 是最有效的,但只有在你有文件创建控制.备选方案 3 通常更有效比备选方案 1,但可能存在某些小树大小,其中 1最好.

I am trying to grab all the files created under a parent directory. The parent directory has a lot of sub directories followed by files in those directories.

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

Currently I am grabbing all the ids of sub folders and constructing a query such as q: 'subfolder1id' in parents or 'subfolder2id' in parents to find the list of files. Then I issue these in batches. If I have 100 folders, I issue 10 search queries for a batch size of 10.

Is there a better way of querying the files using google drive rest api that will get me all the files with one query?

解决方案

Here is an answer to your question.

Same idea from your scenario:

folderA____ folderA1____folderA1a
       \____folderA2____folderA2a
                    \___folderA2b

There 3 alternative answers that I think you can get an idea from.

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 rest 递归搜索文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆