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

标签 google-drive-api

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

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/

相关文章:

android - 使用 Documents List API v3.0 在 Android 手机上使用嵌入 URL 发布预览 Google Docs 文本文档

javascript - 使用 Javascript 中的 Google Drive API 批量插入权限 API

javascript - 简单语音合成 js 使 Chrome 崩溃并显示 Aw Snap 屏幕

c# - 使用 C# 在 ASP.Net 中使用 Google Drive API 将文件上传到 Google Drive

javascript - 谷歌云端硬盘 SDK - JavaScript : How to list SHARED FILES (not shared with me)

google-drive-api - 为什么对gapi.client.drive.realtime.get的响应为空?

javascript - Appcelerator Titanium 上传到谷歌驱动器 - Javascript 中的 Rest API - 错误 404

javascript - 尝试将文件上传到 Google Drive api 时收到 404,怀疑存在数据格式问题

javascript - 使用javascript读取文件并将其发送到服务器

google-apps-script - 如何从具有文件夹结构的 Blob 数组创建 zip