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

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

问题描述

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

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

当前,我正在获取子文件夹的所有ID,并构建一个查询,例如 q:父母中的'subfolder1id'或父母中的'subfolder2id',以查找文件列表.然后,我分批发行这些.如果我有100个文件夹,则会发出10个搜索查询,批处理大小为10.

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.

是否有更好的方法使用Google Drive Rest API查询文件,一次查询即可获取所有文件?

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

推荐答案

这是一个与您的情况相同的想法:

Same idea from your scenario:

folderA____ folderA1____folderA1a
       \____folderA2____folderA2a
                    \___folderA2b

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

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

替代1.递归

诱惑是列出文件夹A的子项,对于任何 属于文件夹的孩子,递归列出他们的孩子,冲洗, 重复.在极少数情况下,这可能是最好的 方法,但是对于大多数情况,它存在以下问题:-

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.

替代2.共同的父母

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

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.

替代3.文件夹优先

首先获取所有文件夹.是的,所有人.一旦拥有全部 在内存中,您可以爬网他们的父母属性并建立 您的树形结构和文件夹ID列表.然后,您可以做一个 files.list?q='folderA' in parents or 'folderA1' in parents or 'folderA1a' in parents....使用此技术,您可以获得 在两个http调用中进行所有操作.

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.

替代2效率最高,但只有在具备以下条件时才有效 控制文件创建.替代方案3通常更有效 比替代方案1小,但可能有一些小树,其中1 最好.

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天全站免登陆