Search code examples
node.jsgoogle-apigoogle-drive-api

Issue with getting the direct children of a drive folder


I am currently running into an issue with the google drive api (using it via Node.js googleapis package, googleapis.google.drive) When I use the drive.list() function, it always returns all children, recursively, so I get all the descendants, rather than just all the children. Alongside that, whenever I use drive.get() with "parents" specified in the fields argument, it always just returns one parent, and unless I filtered for a specific parent, that parent it returns is ALWAYS the root id.

This way I cannot even make a proper file tree in node without making a bunch of api calls for each and every folder separately, and I'm unsure whether I'm simply doing something wrong, or whether the API is simply this limited. Is there any way to actually get direct parent/children?

Example of what I'm running into:

const drive = // in here I have my authorised googleapis.google.drive instance

drive.files.list({
    q: `'${rootid}' in parents`,
    fields: `files(id, name, parents)`
})

My file organisation looks something like this:

"root"
 - "test" (folder, located in root)
  - "test.txt" (txt file, located in test)

The code example above lists both the test folder as well as the test.txt text file, and not only the direct children (in this case just the folder).

Further there is (with the same file structure) an issue with getting the parent of a file, with the following code:

drive.files.get({
    fileId: someFileId,
    fields: `parents`
})

However, this always seems to return the id of the root folder, except when I specify a parent in the q parameter, in which case it only returns the id of the parent I placed in the q parameter, and not any others. This makes it extremely unclear what the actual parent of a file is.

How should I deal with these two problems?

Please let me know if I need to add any info to my question, thanks for reading!

Incase above is not specific enough for my situation, here is a direct copy of the relevant parts of my code:

const { google } = require('googleapis')
const drive = google.drive({
    version: `v3`,
    auth: driveClient,
})

const listFolders = async (foldersOnly = true, parent = `root`) => await drive.files.list({
    q: `'${parent}' in parents ${foldersOnly ? `and mimeType='application/vnd.google-apps.folder'` : ``}`,
    fields: `nextPageToken, files(id, name, parents)`,
})

// actually call the functions
listFolders(false).then((r) => console.log(r?.data?.files)).catch(console.log) // logs result 1
listFolders(false, `1MheLbFrr2LIn_KKa7XHDGvw1NVKIWCvY`).then((r) => console.log(r?.data?.files)).catch(console.log) // logs result 2
drive.files.get({
    fileId: `1eRm-efvklMQcsSggdjorOgsWwmeQ07pu`,
    fields: `parents`,
}).then((r) => console.log(r.data)).catch(console.log) // logs result 3

Results (log):
Result 1:
Result 1
Result 2:
Result 2
Result 3:
Result 3


Solution

  • As I was unable to find a solution to the problem itself, I instead went around it. I made a short function for parsing names and ids of files into an Object, and placed the full path of a file in its name (the limit is somewhere around 32 or 33KB anyway for file names in Google Drive). This way I could get children better and more efficiently with just one request used to make a correct file tree.