Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a list of files by folder on Drive SDK

I am trying to build a web UI for users to navigate his/her Google Drive and select one or more documents to be referenced later on in a website from a DB. I am currently building a web interface using PHP.

The problem I am facing is that I cant find a single function to get a list of files by folder Id.

I tried using:

$service->children->listChildren($rootFolderId)

…but that will only give the files reference ID of the files within the folder (the so called children resource item), which means I have to loop through those files and create a call for each one of them in order to get all the metadata I need for my UI.

Unfortunately..

$service->files->list()

..will list ALL my files with no filtering options by folder.

I would like to know if there is an efficient way of extracting a folder file list from a single call to the Drive server. I actually remember being able to perform this taks over the old Google DOC API.

Thank you very much for your help

like image 637
MightyMouse Avatar asked Jul 09 '12 18:07

MightyMouse


People also ask

How do I get a list of files in a Google Drive folder?

Goto the same Google Sheets File and refresh the page. Then you will get the "List Files/Folders" menu, click on it, and select the "List All Files and Folders" item.

How do you fetch on Google Drive?

On your computer, go to drive.google.com. You'll see "My Drive," which has: Files and folders you upload or sync. Google Docs, Sheets, Slides, and Forms you create.


2 Answers

NB This answer uses the v2 API. New applications should write to the v3 API.

It is now possible to use the drive.files.list endpoint to list files belonging to a specific folder.

To do so, you can use the ?q= query parameter as follows:

GET https://www.googleapis.com/drive/v2/files?q="'<FOLDER_ID>' in parents"

In PHP, that would be

$service->files->list(array('q' => "'<FOLDER_ID>' in parents"));
like image 95
Alain Avatar answered Sep 28 '22 15:09

Alain


Will the query parameter   <FOLDER_ID> in parents   list files and/or folders having such a <FOLDER_ID> in parents?
In my opinion,- this is the expected result. I ask this question because, in the above answer, one reads:

... to list files belonging to a specific folder.

and the documentation about search-parameters explains:

This finds all child folders for the parent folder whose ID is 1234567.

I'll add that a Folder-Only list could be returned by adding something like
and mimeType='application/vnd.google-apps.folder'
to the query string.

like image 45
Pierre Avatar answered Sep 28 '22 17:09

Pierre