简体   繁体   English

将Google Drive API与Jav​​a配合使用,如何仅列出驱动器部分/文件夹中的文件

[英]Using Google Drive API with Java, how to list only files in the Drive Section/Folder

I am trying to get all files that are in a specific section of Google Drive (The Drive Section). 我正在尝试获取Google云端硬盘特定部分(驱动器部分)中的所有文件。

Those are the files I am looking for: 这些是我要找的文件:

在此输入图像描述

You can see a folder named Projet 3 and a file named Processus TP2 questions . 您可以看到名为Projet 3的文件夹和名为Processus TP2 questions的文件Processus TP2 questions In the folder I only have one file. 在文件夹中我只有一个文件。

But when I try to list all the files, I get thousands of files. 但是当我尝试列出所有文件时,我收到了数千个文件。 If I search for them using the search bar on top of Google Drive, it finds them, but I have no idea where they are (maybe Gmail attachement?). 如果我使用Google云端硬盘顶部的搜索栏搜索它们,它会找到它们,但我不知道它们在哪里(也许是Gmail附件?)。

This is my search query: 这是我的搜索查询:

FileList result = service.files().list()
    
    .setQ("mimeType != 'application/vnd.google-apps.folder' 
            and trashed = false")
    
    .setSpaces("drive")
    .setFields("nextPageToken, files(id, name, parents)")
    .setPageToken(pageToken)
    
    .execute();

How can I list only the files I can see in my Drive Section/Folder on the web UI? 如何仅列出我在Web UI上的驱动器部分/文件夹中可以看到的文件?

Thank you very much. 非常感谢你。

Use "parents" property in your query, like this: 在查询中使用“parents”属性,如下所示:

FileList result = service.files().list()
    
.setQ("'root' in parents and mimeType != 'application/vnd.google-apps.folder' and trashed = false")
    
.setSpaces("drive")
.setFields("nextPageToken, files(id, name, parents)")
.setPageToken(pageToken)
    
.execute();

If you want to list the contents of a specific folder rather than root - use that folder's id instead of 'root' in the query. 如果要列出特定文件夹而不是根目录的内容 - 请在查询中使用该文件夹的id而不是“root”。

You may actually retrieve a list of your files by sending HTTP request with all the applicable parameters given in Files: list . 实际上,您可以通过发送带有Files:列表中给出的所有适用参数的HTTP请求来检索文件列表

This sample code is given in the documentation: 此示例代码在文档中给出:

private static List<File> retrieveAllFiles(Drive service) throws IOException {
    List<File> result = new ArrayList<File>();
    Files.List request = service.files().list();

    do {
      try {
        FileList files = request.execute();

        result.addAll(files.getItems());
        request.setPageToken(files.getNextPageToken());
      } catch (IOException e) {
        System.out.println("An error occurred: " + e);
        request.setPageToken(null);
      }
    } while (request.getPageToken() != null &&
             request.getPageToken().length() > 0);

    return result;
  }

Aside from that, you might need to consider using available scopes like requesting full drive scope since you have the need related to listing or reorganizing files in user's Drive as discussed in Choose Auth Scopes . 除此之外,您可能需要考虑使用可用的范围,例如请求完整的驱动器范围,因为您需要与在用户的驱动器中列出或重新组织文件相关,如选择身份验证范围中所述

Request format: 请求格式:

(https://www.googleapis.com/auth/drive)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM