为什么在 Google Drive API 查询时删除的文件仍然显示在结果中

Why deleted file still show up in result while query by Google Drive API

我正在做一个自动生成文件的服务,并使用 Spring 引导和 java 8 将其上传到 google 驱动器。这是另一个更大服务的外部服务。

我使用的 Google 环境:

文件夹报告中有一些文件和文件夹被 UI 使用 google 帐户 A 正常删除(删除然后清空垃圾箱)。当我使用服务帐户密钥 C 进行查询时,我删除的那些文件和文件夹仍然显示在结果中。我已经等了 1 天,但结果仍然包含已删除的文件。

有人可以为我解释 Google 驱动器 API 的这种行为吗?

代码如下:

public List<GoogleFileItem> getAllFile() {
    try {
        if (!serviceAccountKey.exists()) {
            throw new Exception("key file don't exist");
        }
        Drive drive = createDrive();

        List<GoogleFileItem> responseList = new ArrayList<>();

        FileList result = drive.files().list()
                .setQ("trashed=false")
                .setFields("nextPageToken, files(id, name, kind, mimeType, parents, trashed)")
                .execute();
        List<File> files = result.getFiles();
        int i =0 ;
        for (File file : files) {
            if (file.getTrashed()) i++;
            GoogleFileItem item = new GoogleFileItem();
            item.setId(file.getId());
            item.setName(file.getName());
            item.setThumbnailLink(file.getThumbnailLink());
            item.setKind(file.getKind());
            item.setMimeType(GoogleMimeType.find(file.getMimeType()));
            if (file.getParents() != null) {
                item.setParents(file.getParents());
            } else {
                item.setParents(Collections.singletonList(""));
            }
            responseList.add(item);
        }

        return responseList;

    } catch (Exception e) {
        e.printStackTrace();
        logger.error("Exception: " + e);
        return null;
    }
}

编辑:

  • 我遗漏的一个重点是,我删除的文件是使用 API 使用服务帐户密钥 C 上传到 Google 驱动器的,所以这可能是问题的原因,但我不确定

答案:

如果共享文件夹中的文件被协作者(而非所有者)删除,则只会为该协作者删除该文件,而不会为其他人删除。根据文档,只有所有者可以访问此文件,但其他协作者(尚未主动删除该文件)似乎也可以访问:

When someone deletes a file from a shared folder, only the owner can access it.

因此,当协作者(不是所有者)尝试从 UI 中的共享文件夹中删除文件时,会显示以下消息:

"{YOUR_FILE_NAME}" will be removed from view. Collaborators will still have access.

因为文件是由服务账号上传的,所以这个账号是文件的拥有者。即使协作者(您的常规帐户)删除了它,服务帐户(作为所有者)仍然可以访问它。

参考: