Andrés
Andrés

Reputation: 305

Google Drive API just listing up to 100 files per folder

I'm creating an application to backup files stored in Google Drive using Python. The fist problem I encountered is that for some reason it just list up to 100 files in every folder. I think I'm using the correct scope ('https://www.googleapis.com/auth/drive') and here is the line of code I'm using to list the contents of a folder

folder_contents = service.files().list( q="'%s' in parents" % folder['id'] ).execute()

Is there any way to change the number of listed files? I found in this thread Google Drive List API - not listed all the documents? that there is a way to do this but NumberToRetrieve does not work. I tried to test it in the Drive API reference webpage (https://developers.google.com/drive/v2/reference/files/list) but it gives me an error. I also can't figure out how to put this parameter when I request the list fromm my code (I'm new using the Drive API).

Thanks in advance.

EDIT: I found a way to increase the number of files using the MaxResuls flag. But it just allows to list up to 1000 and that's not quite enough.

Upvotes: 1

Views: 3451

Answers (3)

Alejandro Robles
Alejandro Robles

Reputation: 41

"results = service.files().list( pageSize = 1000, q="'xxx' in parents",fields="nextPageToken, files(id, name)").execute()"

This worked for me.

Upvotes: 1

ahumblenerd
ahumblenerd

Reputation: 225

Default max results of query is 100. Must use pageToken/nextPageToken to repeat it. see Python Google Drive API - list the entire drive file tree

Upvotes: 0

pinoyyid
pinoyyid

Reputation: 22286

It's pretty standard for all REST APIs to paginate their output lists. The Drive API is no different. You simply need to monitor the presence of a nextPageToken in the JSON response and recurse through the successive pages.

NB. Don't make the mistake as some have of assuming that a response with less than MaxResults is the last page. You should rely solely on nextPageToken.

As an aside, once you have pagination working in your code, consider setting maxResults back to its default. I have a suspicion that the non-default values are less well tested, and also more prone to timeouts.

Upvotes: 6

Related Questions