且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

google drive API搜索具有给定文件夹ID的所有子文件

更新时间:2023-02-14 13:08:54

如果我对您的理解正确,则希望:

If I understand you correctly, you want to:

  • 检索特定文件夹的后代(子代,孙子代等)的所有图像文件.
  • 您想通过一个API调用来检索它.

当前无法通过单个调用在子文件夹中检索文件.这是因为文件资源(而文件夹"是 File )不具有有关此父文件夹中包含哪些文件的信息(更不用说包含在子文件夹"中的文件了),并且仅具有有关此文件夹的直接父项的信息,而没有可以这么说.祖父母等等.

It is currently not possible to retrieve files in sub-folders with a single call. That's because a File resource (and a "folder" is a File) does not have information on what files are contained in this parent folder (let alone files contained in "sub-folders") and it only has information on the immediate parents of this folder, and not grandparents and so on, so to speak.

为了检索所有后裔",某个文件夹,可以使用两种主要方法(主要基于 pinoyyid的答案):

In order to retrieve all "descendants" of a certain folder, two main methods can be used (largely based on this answer by pinoyyid):

  • 通过文件:列表列出您云端硬盘中的所有文件夹>,而不考虑其父母中是否具有主文件夹ID.我们将此列表称为所有文件夹.
  • 根据是否在父文件夹中具有主文件夹ID,
  • 过滤所有文件夹.我们将此列表称为直接子代.
  • 对于直接子代中的每个元素,请根据其在父代中是否具有相应的直接子代子来过滤所有文件夹.这样,您将检索孙代人的列表.对每个 Grandchildren 元素重复相同的过程,然后为每个 Great-grandchildren 重复此类操作,直到没有文件夹为止.这样,您将检索到主文件夹ID的所有 descendant 文件夹.
  • 一旦有了所有 descendant 文件夹,就必须制作另一个 Files:list 来查找这些文件夹中的任何子文件夹的图像,或者从主文件夹中文件夹.搜索查询可能是这样的:
  • List all folders in your Drive via Files: list, disregarding whether they have the main folder ID in parents. Let's call this list All folders.
  • Filter All folders, according to whether they have the main folder ID in parents. Let's call this list Direct children.
  • For each element in Direct children, filter All folders according to whether they have the corresponding direct child in parents. This way, you will retrieve a list of Grandchildren. Repeat the same process for each Grandchildren element, then for each Great-grandchildren and so on, until no folders are left. With this, you will have retrieved all the folders which are descendant of the main folder ID.
  • Once you have all the descendant folders, you have to make another Files: list to find the images which are children of any of those folders, or from the main folder. The search query could be something like this:
mimeType contains 'image/' AND ('mainFolderId' in parents OR 'descendant1ID' in parents OR 'descendant2ID' in parents OR...)

  • 注意:此方法需要较少的API调用,如果文件树较大,则更合适.
    • Note: This method requires less API calls and will be more appropriate if the file tree is relatively large.
      • 使用文件:列表列出其中的所有文件您的主文件夹是文件夹(因此应该列出自己的子文件夹)或图像.在这种情况下,搜索查询将类似于:
      • Use Files: list to list all files in your main folder which are either folders (and so their own children should be listed) or images. In this case, the search query would be something like:
(mimeType = 'application/vnd.google-apps.folder' OR mimeType contains 'image/') AND 'folderId' in parents

  • 对于此调用返回的每个孩子,检查它是图像(MIME类型包含 image/)还是文件夹(MIME类型: application/vnd.google-apps.folder ).对于每个文件夹,使用上一步中的方法列出其中包含的所有文件.
  • 重复这两个步骤,直到没有文件夹为止.您检索到的所有图像都是后代"图像.您的主文件夹中.
    • For each child returned by this call, check if it's an image (MIME type containing image/) or a folder (MIME type: application/vnd.google-apps.folder). For each folder, list all files contained in it, using the method from previous step.
    • Repeat these two steps until there is no folder left. All the images you have retrieved are the "descendants" of your main folder.
    • 问题跟踪器中有一个开放的功能请求,在查询搜索中包括祖先,这可以允许在子文件夹中检索子代,并使整个过程更加容易.您可以单击所引用页面左上角的星号,以跟踪此请求并确定优先级:

      There is an open feature request in Issue Tracker to include ancestors in query searches, which could allow the retrieval of children in subfolders, and making this whole process much more easy. You could click the star on the top left of the referenced page in order to keep track of this request and to help prioritize it: