Wednesday, 12 June 2019

Google Drive API: check if folder exists

I'm using the Google Drive API to try to answer a seemingly simple question: does a folder by a certain name exist in a drive?

Specifics:

  • Version V3 of the drive API
  • Python client: googleapiclient

Example:

Given the enclosing drive ID abcdef, does the folder with name June 2019 (and mimeType application/vnd.google-apps.folder) exist?

Current route:

>>> from googleapiclient.discovery import build
>>> # ... build credentials
>>> driveservice = build("drive", "v3", credentials=cred).files()
>>> [i for i in driveservice.list().execute()['files'] if 
...  i['name'] == 'June 2019' and i['mimeType'] == 'application/vnd.google-apps.folder']                                                                                                                                                                     
[{'kind': 'drive#file',
  'id': '1P1k5c2...........',
  'name': 'June 2019',
  'mimeType': 'application/vnd.google-apps.folder'}]

So the answer is yes, the folder exists. But there should be a more efficient way to do this via .list() by passing the driveId. How can that be done? I've tried various combinations all of which seem to throw a non-200 response.

>>> FOLDER_ID = "abcdef........"
>>> driveservice.list(corpora="drive", driveId=FOLDER_ID).execute()                                                                                                                                                                                                          
# 403 response, even when adding the additional requested params

How can I use the q param to query by folder name?



from Google Drive API: check if folder exists

No comments:

Post a Comment