google drive API搜索具有给定文件夹ID的所有子文件 [英] google drive API search all children file with given folder ID

查看:58
本文介绍了google drive API搜索具有给定文件夹ID的所有子文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Google云端硬盘中的文件夹的结构:

Here is how my folder is structured in google drive:

Picture
    |----------Date1
                 |-----Pic1.png
                 |-----Pic2.png
    |----------Date2
                 |-----Pic3.png
                 |-----Pic4.png

现在,我只有图片"文件夹的ID(parentID文件夹).现在,我想获取Pic1图片(在Date1文件夹中).是否可以仅查询1次以获取仅具有Picture文件夹ID的图片,否则我将不得不多次查询(获取Date1和Date2文件夹ID,然后继续查询)?

Right now I only have the ID of Picture folder (the parentID folder). Now I want to get Pic1 picture (inside Date1 folder). Is it possible to query only 1 time to get the picture with only the Picture folder's ID or I will have to query multiple times (get the Date1 and Date2 folder ID, then continue querying) ?

这是我的代码

$imageName= "Pic1" // name of the image I want to find, which is Pic1
$folderId = "PictureFolderId" // Folder Picture's ID
$optParams = array(
            'pageSize' => 1,
            'fields' => 'nextPageToken, files(contentHints/thumbnail,fileExtension,id,name,size)',
            'q' =>"mimeType contains 'image/' AND name contains '".$imageName."' AND '".$folderId."' in parents"
          );
          $results = $googleDriveService->files->listFiles($optParams);
          
        if (count($results->getFiles()) == 0) {
            print "No files found.\n";
        } else {
            // Do something.
        }

推荐答案

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

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):

  • 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:

          这篇关于google drive API搜索具有给定文件夹ID的所有子文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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