2

我在 Play Framework 应用程序中使用 Google Drive API 库。

我正在尝试搜索团队云端硬盘,检查文件夹是否存在,如果不存在则创建文件夹。我在FileList之前使用该对象TeamDriveList,但使用FileList是在团队驱动器中不断创建该文件夹,即使它存在 - 它永远不会找到现有文件夹。它会一遍又一遍地复制文件夹。

因此,在阅读了一些帖子后:

迭代 Team Drive 文件 Google Drive API JAVA

Google Drive API - 创建重复文件夹

看来我需要切换到TeamDriveList.

我在谷歌的文档中使用这个例子: https ://developers.google.com/drive/v3/web/manage-teamdrives#managing_team_drives_for_domain_administrators

如您所见,它用于setQ执行搜索:

TeamDriveList result = driveService.teamdrives().list()
      .setQ("organizerCount = 0")
      .setFields("nextPageToken, teamDrives(id, name)")
      .setUseDomainAdminAccess(true)
      .setPageToken(pageToken)
      .execute();

但是,在我的代码中,当我尝试编译时,它会抛出一个setQ不存在的错误。

这是我的代码:

public static String getSubfolderID(Drive service, String parentFolderID, String folderKeyToGet) {
    // We need to see if the folder exists based on the ID...
    String folderID = "";
    Boolean foundFolder = false;
    TeamDriveList result = null;
    File newFolder = null;

    // Set the drive query...
    String driveQuery = "mimeType='application/vnd.google-apps.folder' and '" + parentFolderID
            + "' in parents and name contains '" + folderKeyToGet
            + "' and trashed=false";

    try {
        result = service.teamdrives().list()
                .setQ(driveQuery)
                .execute();
    } catch (IOException e) {
        e.printStackTrace();
    }
    for (TeamDrive folder : result.getTeamDrives()) {
        System.out.printf("Found folder: %s (%s)\n", folder.getName(), folder.getId());
        foundFolder = true;
        folderID = folder.getId();
    }
    if (foundFolder != true) {
        // Need to create the folder...
        File fileMetadata = new File();
        fileMetadata.setName(folderKeyToGet);
        fileMetadata.setTeamDriveId(parentFolderID);
        fileMetadata.set("supportsTeamDrives", true);
        fileMetadata.setMimeType("application/vnd.google-apps.folder");
        fileMetadata.setParents(Collections.singletonList(parentFolderID));

        try {
            newFolder = service.files().create(fileMetadata)
                    .setSupportsTeamDrives(true)
                    .setFields("id, parents")
                    .execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Send back the folder ID...
        folderID = newFolder.getId();
        System.out.println("Folder ID: " + newFolder.getId());
    }

    return folderID;
}

有问题的代码行是:

    result = service.teamdrives().list()
            .setQ(driveQuery)
            .execute();

如果这不存在,我该如何搜索,使用这个查询:

String driveQuery = "mimeType='application/vnd.google-apps.folder' and '" + parentFolderID
        + "' in parents and name contains '" + folderKeyToGet
        + "' and trashed=false";

对于我正在寻找的文件夹并确保我找到该文件夹​​以免导致重复?

感谢帮助。

4

0 回答 0