1
votes

I am using the Google Drive API Client Library for Python.

I want to extract the metadata only for current files in 'My Drive'. However, using service.files().list().execute() yields a list not just of files currently shown in 'My Drive', but also previously deleted files and also files stored in 'Google Photos' and 'Recent'.

How can I filter the list of files received to retrieve only the files that would be downloaded onto a client with Google Drive sync installed?

2

2 Answers

1
votes

Updated

I have found a better way to get the list of items in "My Drive". Instead of file.list, change.list fits better in our use case.

By setting includeDeleted and includeSubscribed as false, change.list returns a list of changes that holds files that are strictly located in the user's Drive (no Recents and Shared With Me). However, it does include trashed items.

This response is easier to work with since we only have to filter the items labelled as thrashed. It is still not perfect, but at least it saves us the trouble to construct a tree.

Original Answer

The closest way that I have found is to query for 'me' in owners and trashed = false. This query will return all files in "My Drive" and (unfortunately) some files in "Shared With Me". Then you can construct a tree from root folder and drop those that are orphaned.

On the other hand, the cleanest way is to query for 'root' in parents and traverse the tree layer by layer. That's not so efficient though.

Note: query not sharedWithMe or sharedWithMe = false results in 400, otherwise it will be the best solution.

0
votes

You could use both Parent and Child resources, to clearly identify what the contents of the 'My Drive' as a folder has. Additionally, you can use the files.list method while including a query to exclude the trashed items. Just include the 'q' parameter, and list the query as a string:

param = {}
if page_token:
    param['pageToken'] = page_token
param['q'] = 'trashed = false'
files = service.files().list(**param).execute()

You can find more information on the available values for the query here.