Reputation: 42743
I use below function to get folder content (files list) from google drive (API V3):
def get_gdrive_content(folder_id):
ret_val = []
page_token = None
while True:
response = service.files().list(q=f"parents = '{folder_id}'",
fields='nextPageToken, files(id, name, mimeType)',
pageToken=page_token
).execute()
for file in response.get('files', []):
ret_inner = {'file_name': file.get('name'), 'mime_type': file.get('mimeType'), 'file_id': file.get('id')}
ret_val.append(ret_inner)
page_token = response.get('nextPageToken', None)
if page_token is None:
break
return ret_val
This works and I get files list, just with one problem: if I remove file on google drive, this function still returns that removed file(s).
May be there is some timeout on Gdrive for removed files? I just can't found about this: here
I didn't searched good enough in docs? or I have something wrong in code? Any help very appreciated!
Upvotes: 1
Views: 74
Reputation: 1878
According to the documentation, you might want to add and thrashed = false
to your query:
def get_gdrive_content(folder_id):
ret_val = []
page_token = None
while True:
response = service.files().list(
q=f"parents = '{folder_id}' and thrashed = false",
fields='nextPageToken, files(id, name, mimeType)',
pageToken=page_token
).execute()
for file in response.get('files', []):
ret_inner = {
'file_name': file.get('name'),
'mime_type': file.get('mimeType'),
'file_id': file.get('id')
}
ret_val.append(ret_inner)
page_token = response.get('nextPageToken', None)
if page_token is None:
break
return ret_val
Upvotes: 3