Reputation: 1133
What is the best way to list all the files of a specific google drive directory by folder ID. If I build a service like below, what is the next step? COuldn't findanything that worked for me. Service_Account_File in thsi example is a json file with tokens.
SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = service_account_file
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = discovery.build('drive', 'v3', credentials=credentials)
Upvotes: 1
Views: 7914
Reputation: 201378
I believe your goal as follows.
In this case, I would like to propose the following 2 patterns.
In this pattern, the method of "Files: list" in Drive API with googleapis for python is used. But in this case, the files in the subfolders in the specific folder are not retrieved.
from google.oauth2 import service_account
from googleapiclient.discovery import build
SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = service_account_file
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
service = build('drive', 'v3', credentials=credentials)
topFolderId = '###' # Please set the folder of the top folder ID.
items = []
pageToken = ""
while pageToken is not None:
response = service.files().list(q="'" + topFolderId + "' in parents", pageSize=1000, pageToken=pageToken, fields="nextPageToken, files(id, name)").execute()
items.extend(response.get('files', []))
pageToken = response.get('nextPageToken')
print(items)
q="'" + topFolderId + "' in parents"
means that the file list is retrieved just under the folder of topFolderId
.pageSize=1000
is used, the number of use of Drive API can be reduced.In this pattern, a library of getfilelistpy is used. In this case, the files in the subfolders in the specific folder can be also retrieved. At first, please install the library as follows.
$ pip install getfilelistpy
The sample script is as follows.
from google.oauth2 import service_account
from getfilelistpy import getfilelist
SCOPES = ['https://www.googleapis.com/auth/drive']
SERVICE_ACCOUNT_FILE = service_account_file
credentials = service_account.Credentials.from_service_account_file(SERVICE_ACCOUNT_FILE, scopes=SCOPES)
topFolderId = '###' # Please set the folder of the top folder ID.
resource = {
"service_account": credentials,
"id": topFolderId,
"fields": "files(name,id)",
}
res = getfilelist.GetFileList(resource)
print(dict(res))
Upvotes: 5
Reputation: 116869
The file.list method has a parameter called q. you can use the q to search for things like files with in a directory.
Assuming you know the file id of the folder you are looking it you would do "parents in folderid"
This will return all the files within that folder.
page_token = None
while True:
response = drive_service.files().list(q="parents in 'YOURFOLDERIDHERE'",
spaces='drive',
fields='nextPageToken, files(id, name)',
pageToken=page_token).execute()
for file in response.get('files', []):
# Process change
print 'Found file: %s (%s)' % (file.get('name'), file.get('id'))
page_token = response.get('nextPageToken', None)
if page_token is None:
break
Upvotes: 1