php 在 Drive SDK 上按文件夹获取文件列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11400669/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:27:16  来源:igfitidea点击:

Getting a list of files by folder on Drive SDK

phpgoogle-drive-api

提问by MightyMouse

I am trying to build a web UI for users to navigate his/her Google Drive and select one or more documents to be referenced later on in a website from a DB. I am currently building a web interface using PHP.

我正在尝试为用户构建一个 Web UI,以便用户导航他/她的 Google Drive 并选择一个或多个稍后在网站中从数据库中引用的文档。我目前正在使用 PHP 构建 Web 界面。

The problem I am facing is that I cant find a single function to get a list of files by folder Id.

我面临的问题是我找不到一个函数来按文件夹 Id 获取文件列表。

I tried using:

我尝试使用:

$service->children->listChildren($rootFolderId)

…but that will only give the files reference ID of the files within the folder (the so called children resource item), which means I have to loop through those files and create a call for each one of them in order to get all the metadata I need for my UI.

…但这只会给出文件夹中文件的文件引用 ID(所谓的子资源项),这意味着我必须遍历这些文件并为每个文件创建一个调用以获取所有元数据我需要我的用户界面。

Unfortunately..

很遗憾..

$service->files->list()

..will list ALL my files with no filtering options by folder.

..将按文件夹列出我的所有文件,没有过滤选项。

I would like to know if there is an efficient way of extracting a folder file list from a single call to the Drive server. I actually remember being able to perform this taks over the old Google DOC API.

我想知道是否有一种有效的方法可以从对 Drive 服务器的单个调用中提取文件夹文件列表。我实际上记得能够通过旧的 Google DOC API 执行此任务。

Thank you very much for your help

非常感谢您的帮助

回答by Alain

NB This answer uses the v2 API. New applications should write to the v3 API.

注意此答案使用 v2 API。新应用程序应写入 v3 API。

It is nowpossible to use the drive.files.list endpoint to list files belonging to a specific folder.

这是目前可以使用drive.files.list端点属于一个特定的文件夹列表中的文件。

To do so, you can use the ?q=query parameter as follows:

为此,您可以?q=按如下方式使用查询参数:

GET https://www.googleapis.com/drive/v2/files?q="'<FOLDER_ID>' in parents"

In PHP, that would be

在 PHP 中,这将是

$service->files->list(array('q' => "'<FOLDER_ID>' in parents"));

回答by Pierre

Will the query parameter   <FOLDER_ID> in parents  list files and/or foldershaving such a <FOLDER_ID> in parents?
In my opinion,- this is the expected result. I ask this question because, in the above answer, one reads:

查询参数是否会   <FOLDER_ID> in parents  列出父项中具有此类 <FOLDER_ID> 的文件和/或文件夹
在我看来,-这是预期的结果。我问这个问题是因为在上面的答案中,有人写道:

... to list filesbelonging to a specific folder.

......到列表的文件属于一个特定的文件夹。

and the documentation about search-parametersexplains:

关于搜索参数文档解释了:

This finds all child foldersfor the parent folder whose ID is 1234567.

这将查找ID 为 1234567 的父文件夹的所有子文件夹。

I'll add that a Folder-Only list could be returned by adding something like
and mimeType='application/vnd.google-apps.folder'
to the query string.

我将补充一点,可以通过向
and mimeType='application/vnd.google-apps.folder'
查询字符串添加类似内容来返回仅文件夹列表。

回答by Radu Simionescu

No need to use search queries. Simply make a call to GET https://www.googleapis.com/drive/v2/files/{folderId}/children

无需使用搜索查询。只需拨打 GET https://www.googleapis.com/drive/v2/files/{folderId}/children

https://developers.google.com/drive/v2/reference/children/list

https://developers.google.com/drive/v2/reference/children/list

回答by Pir Fahim Shah

Its easy now, don't worry.If you want to get all files and folder from specific FOLDER, then just copy and paste this code in your project and it will retrieve all the files and folder from the specific Folder. Note: You should have your own Folder ID

现在很简单,不用担心。如果您想从特定文件夹中获取所有文件和文件夹,那么只需将此代码复制并粘贴到您的项目中,它就会从特定文件夹中检索所有文件和文件夹。注意:您应该有自己的文件夹 ID

 List<File> result = new ArrayList<File>();
    Files.List request = null;

    try {
          request = mService.files().list();//plz replace your FOLDER ID in below line
          FileList files = request.setQ("'"+MyFoler_Id+"' in parents and trashed=false"").execute();
          result.addAll(files.getItems());          
          request.setPageToken(files.getNextPageToken());
        } 
      catch (IOException e)
      {
        System.out.println("An error occurred: " + e);
        request.setPageToken(null);
      }

//Print Out the Files and Folder Title which we have retrieved.

//打印出我们检索到的文件和文件夹标题。

  for(File f:result)
  {
      System.out.println("My recvd data "+f.getTitle());
  }