1

I have a Google Drive that is conveniently organized and has sub-folders within it's shared drive. I am attempting to parse through all, or specified, folders and retrieve their contents, or images. I have working code for retrieving all folder ids given a root folder id, and I have working code for retrieving files in one folder.

My issue is that when I attempt to string together the query to retrieve files within any of the specified folder ids, the following is returned in the API response: an empty files array despite there being images in each respective folder, or a files array containing only folder mime-types and not a single image.

I have been stuck on this issue trying everything from changing my permission scope, different folders, trying to Frankenstein together a query, but nothing as gotten me closer.


Here is an example of my query: '<folder-id-1>' in parents or '<folder-id-2>' in parents.

And the snippet from my code:

const res = await this.drive.files.list({
    q: `(${encodeURIComponent(foldersChunk.map(folder => `'${folder.id}' in parents`).join(' or '))})`
});

Do I have to query each individual folder one-by-one? I was hoping on finding a better solution so I don't have to make excessive API calls to Google or potential slow down the application for the user by having to request each individual folder, as opposed to getting images in multiple folders as once.

FWIW: I have taken a look at this post on S.O. which is where I got the idea in the first place. Another user made a comment about how a similar query, nesting <folder-id> in parents together, worked for them, but like I explained, it hasn't for me.

Infinity
  • 31
  • 1
  • 6

2 Answers2

1

The google-api-nodejs-client has a bug for what I could check trying your code because it's not returning anything at all as in your case, but then I used the Try this API for troubleshooting purposes. In that way, setting the value of the q parameter as in your call ('<folder-id-1>' in parents), it will work as intended.

Therefore, as a workaround, you can use axios and then build the call as it follows:

const queryFoldersWithAxios = async () => {
  const dummyData = [{id: "folder-id-1"}, {id: "folder-id-2"}];
  const params = {
    q: dummyData.map(folder => `'${folder.id}' in parents`).join(' or ')
  };
  const headers = {
    "Authorization": "Bearer <YOUR_ACCESS_TOKEN>", 
    "Content-Type": "application/json"
  }
  return await axios.get("https://www.googleapis.com/drive/v3/files", {params, headers})
}

Notice

  • You can use Google OAuth 2.0 Playground to easily get an access token for testing.

  • I created an issue on Github reporting the bug, you can check it here.

alberto vielma
  • 2,302
  • 2
  • 8
  • 15
  • Unfortunately this isn't a workaround for me. I have tried your work around and I still get the exact same output as I did previously, which was returning a single folder, rather than all items them have in common. I tried it once more using two other folders **that do not have nested folders within them**, and nothing was returned when I should have received 40 images, using both your _workaround_ and Drive from `googleapis` – Infinity Feb 14 '20 at 08:47
  • On second thought, I looked over the issue you made on GitHub again, but I now realize the issue and you're right: when using `google.drive` from `googleapis`, when you try to list all files in multiple collections using `'' in parents or '' in parents or...`, behaves very inconsistently. I explained the inconsistency in detail [over on the issue you created on GitHub](https://github.com/googleapis/google-api-nodejs-client/issues/1957#issuecomment-586180748). – Infinity Feb 14 '20 at 09:43
1

TLDR; add a parameter corpora=allDrives

I'd just had a similar issue. When querying the files list with a single 'folderId1' in parents I received all three files contained within. when I queried for two folders 'folderId1' in parents or 'folderId2' in parents I only recieved two results. The second folder is empty but shouldn't reduce the list from the first folder. When the second folder contained files the results included those but still was missing the third file from the original list.

I also discovered a post discussing an alternative syntax parents in 'folderId' which when I tried it for the single folder only returned the two files.

Fascinating.

After further tinkering I'd discovered that setting the parameter corpora=allDrives resulted in the full three files returned in both versions above.

My guess is that the API assumes the corpora based on the query. In my instance the various folders were from separate Team/Shared Drives. For a single folder it sets it to drive as that covers its need. For multiple folders that span multiple drives it cannot use drive so it sets it to user which only returns files created, opened, or directly shared with the user. For allDrives it needs to be explicitly set.

tzvi
  • 471
  • 3
  • 5
  • 19