如何在Google云端硬盘中搜索子文件夹和子子文件夹? [英] How do I search sub-folders and sub-sub-folders in Google Drive?

查看:194
本文介绍了如何在Google云端硬盘中搜索子文件夹和子子文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个常见问题。



这种情况是: -
$ b $

  folderA ____ folderA1____folderA1a 
\ ____ folderA2____folderA2a
\ ___ folderA2b

。 ..问题是如何列出根目录下的所有文件夹中的所有文件 folderA

解决方案

首先要了解的是,在Google云端硬盘中,文件夹不是文件夹!

我们都习惯于在Windows / nix等文件夹(又名目录)的想法。在现实世界中,文件夹是一个容器,文件放置在其中。也可以在较大的文件夹中放置较小的文件夹。因此,可以将大文件夹视为包含其较小子文件夹中的所有文档。



但是,在Google Drive中,文件夹 NOT <一个容器,以至于在Google Drive的第一个版本中,它们甚至不被称为文件夹,它们被称为集合。文件夹只是一个文件,其中包含(a)没有内容,以及(b)特殊的MIME类型(application / vnd.google-apps.folder)。文件夹的使用方式与使用标签(也称为标签)的方式完全一样。理解这个最好的方法是考虑GMail。如果您查看打开的邮件项目的顶部,您会看到两个图标。工具提示移至的文件夹和工具提示标签的标签。点击其中任何一个,出现相同的对话框,全部是关于标签。您的标签在左侧列出,树形显示器看上去很像文件夹。重要的是,一个邮件项目可以有多个标签,或者你可以说,一个邮件项目可以在多个文件夹中。 Google云端硬盘文件夹的工作方式与GMail标签的工作方式完全相同。

确定文件夹只是一个标签后,没有什么能够阻止您在类似于文件夹树的层次结构中组织标签,实际上这是最常见的这样做。



现在应该清楚folderA2b中的文件(我们称之为MyFile)不是folderA的子代或孙代。它只是一个带有folderA2b标签(令人困惑地称为父项)的文件。



OK,那么如何获取folderA下的所有文件?

选择1.递归



诱惑将列出folderA的子项,对于任何作为文件夹的子项,递归地列出子项,冲洗,重复。在极少数情况下,这可能是最好的方法,但对于大多数情况,它有以下问题: -


  • 它是为每个子文件夹执行服务器往返操作会非常耗时。这当然取决于你的树的大小,所以如果你可以保证你的树大小很小,它可以确定。



< 替代2.共同父母



如果所有文件都是由您的应用程序创建的(即,您正在使用drive.file作用域)。除上面的文件夹层次结构外,还要创建一个名为sayMyAppCommonParent的虚拟父文件夹。当您将每个文件创建为其特定文件夹的子文件夹时,您还将它作为MyAppCommonParent的子文件夹。如果您记得将文件夹视为标签,这会变得更加直观。您现在可以通过简单地在父母中查询 MyAppCommonParent

替代3.首先选择文件夹



首先获取所有文件夹。是的,所有这些。将所有内容都存储起来之后,您就可以抓取父项属性并构建树结构和文件夹ID列表。然后,您可以在父母或单亲家庭中使用单个 files.list?q ='folderA'或父母中的'folderA1'或父母中的'folderA1a'。使用这种技术,您可以在两次http调用中获得所有内容。

方法2是最有效的,但只有在您控制文件创建时才有效。替代方案3通常比替代方案1效率更高,但可能会有某些小的树大小,其中1是最好的。

This is a commonly asked question.

The scenario is:-

folderA____ folderA1____folderA1a
       \____folderA2____folderA2a
                    \___folderA2b

... and the question is how do I list all the files in all of the folders under the root folderA.

解决方案

The first thing to understand is that in Google Drive, Folders are not folders!

We are all used to the idea of folders (aka directories) in Windows/nix etc. In the real world, a folder is a container, into which documents are placed. It is also possible to place smaller folders inside bigger folders. Thus the big folder can be thought of as containing all of the documents inside its smaller children folders.

However, in Google Drive, a Folder is NOT a container, so much so that in the first release of Google Drive, they weren't even called Folders, they were called Collections. A Folder is simply a File with (a) no contents, and (b) a special mime-type (application/vnd.google-apps.folder). The way Folders are used is exactly the same way that tags (aka labels) are used. The best way to understand this is to consider GMail. If you look at the top of an open mail item, you see two icons. A folder with the tooltip "Move to" and a label with the tooltip "Labels". Click on either of these and the same dialogue box appears and is all about labels. Your labels are listed down the left hand side, in a tree display that looks a lot like folders. Importantly, a mail item can have multiple labels, or you could say, a mail item can be in multiple folders. Google Drive's Folders work in exactly the same way that GMail labels work.

Having established that a Folder is simply a label, there is nothing stopping you from organising your labels in a hierarchy that resembles a folder tree, in fact this is the most common way of doing so.

It should now be clear that a file (let's call it MyFile) in folderA2b is NOT a child or grandchild of folderA. It is simply a file with a label (confusingly called a Parent) of "folderA2b".

OK, so how DO I get all the files "under" folderA?

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云端硬盘中搜索子文件夹和子子文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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