1

I am doing a service automatic generate file and upload it to google drive using Spring boot and java 8. This is an external service of another bigger service.

The Google environment I'm using:

  • Google account A with 1 folder name: reports
  • Service account B with the highest access and modifier to folder reports in google account A
  • Service account key C was created by service account B

There was some file and folder inside folder reports that I was deleted normally by UI using google account A (remove then empty trash). While I'm query using service account key C, those file and folder that I was deleted still show up in the result. I have wait 1 day but the result still contain deleted file.

Can somebody explain for me about this behavior of Google Drive API?

Here are the code :

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;
    }
}

Edit:

  • One important point I'm missing is that the file I deleted was upload to Google drive using API use service account key C so it might be the cause of problem but I'm not sure
Dattq2303
  • 302
  • 2
  • 13
  • How did you share that file between the uploader (`service account key C`) and your regular account (`Google account A`). Is this seen under `Shared with me`? Or it's in a shared drive? Please note that if you delete a file that's been shared with you, you're just removing it from view, but it will still show up in search results and you can see it under `Shared with me` again if you open it (ref https://support.google.com/drive/answer/2375057). Possibly that's what's happening here? – Iamblichus Jun 10 '21 at 07:23
  • @lamblichus My case is uploaded by the service account to a shared folder on the user's Drive and also i have check the shared with me again and my deleted file not there – Dattq2303 Jun 10 '21 at 07:28
  • Have you tried looking for it using the search bar on `account A` UI? – Iamblichus Jun 10 '21 at 07:32
  • @lamblichus Yes, I have already try it. – Dattq2303 Jun 10 '21 at 07:33
  • 1
    Probably this is deleted for `account A`, but it's still in the service account's Drive. Have you checked whether you can retrieve that file via API using `account A`? (btw I'm not sure I understand what you mean by `Service account key C was created by service account B`). What does `key C` mean? – Iamblichus Jun 10 '21 at 10:46
  • 1
    @lamblichus Yes, I believe that was the case. Thank you. About the service account key C, that is the **service account key** created by **service account** B. – Dattq2303 Jun 10 '21 at 10:51
  • @lamblichus Yes, I think your comment + explain in your answer is very helpful. – Dattq2303 Jun 10 '21 at 16:08

1 Answers1

3

Answer:

If a file in a shared folder is deleted by a collaborator (not the owner), it's only deleted for that collaborator, and not for the others. According to the docs, only the owner would have access to this file, but other collaborators (which haven't actively deleted that file) seem to have access too:

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

Because of this, when a collaborator (not owner) tries to remove a file from a shared folder in the UI, the following message is displayed:

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

Because the file was uploaded by the service account, this account is the file owner. Even if a collaborator (your regular account) deletes it, the service account (being the owner) can still access it.

Reference:

Iamblichus
  • 18,540
  • 2
  • 11
  • 27