我有与该帖子问的完全相同的问题:List files and folders in a google drive folder 我没有在Google Drive Rest API文档中弄清楚如何获取Google Drive文件夹中的文件列表
答案 0 :(得分:1)
有关可用功能,请参见API ...
您可以使用Drive API files:list方法搜索文件。您可以不带任何参数的情况下调用Files.list,这将返回用户驱动器上的所有文件。默认情况下,Files.list仅返回资源的属性子集。如果要返回更多属性,请使用fields参数指定查询字符串q中要返回的属性。为了使搜索查询更具体,可以对每个查询属性使用多个运算符。
答案 1 :(得分:1)
您可以在此处查看如何列出Drive中的文件的示例:https://developers.google.com/drive/api/v3/search-files。您需要构造一个查询,该查询列出文件夹中的文件:使用
TableView
其中1234是要列出的文件夹的ID。您可以修改查询以列出特定类型的所有文件(例如文件夹中的所有jpeg文件)等。
答案 2 :(得分:0)
这是一个骇人的成功解决方案。实际上,这会从特定的Google云端硬盘文件夹(在本例中为“缩略图”文件夹)获取所有文件。我需要从一个特定的文件夹中获取(不仅仅是列出)所有文件,并对它们进行图像调整,所以我使用了以下代码:
`# First, get the folder ID by querying by mimeType and name
folderId = drive.files().list(q = "mimeType = application/vnd.google-apps.folder' and name = 'thumbnails'", pageSize=10, fields="nextPageToken, files(id, name)").execute()
# this gives us a list of all folders with that name
folderIdResult = folderId.get('files', [])
# however, we know there is only 1 folder with that name, so we just get the id of the 1st item in the list
id = folderIdResult[0].get('id')
# Now, using the folder ID gotten above, we get all the files from
# that particular folder
results = drive.files().list(q = "'" + id + "' in parents", pageSize=10, fields="nextPageToken, files(id, name)").execute()
items = results.get('files', [])
# Now we can loop through each file in that folder, and do whatever (in this case, download them and open them as images in OpenCV)
for f in range(0, len(items)):
fId = items[f].get('id')
fileRequest = drive.files().get_media(fileId=fId)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, fileRequest)
done = False
while done is False:
status, done = downloader.next_chunk()
fh.seek(0)
fhContents = fh.read()
baseImage = cv2.imdecode(np.fromstring(fhContents, dtype=np.uint8), cv2.IMREAD_COLOR)
答案 3 :(得分:0)
# Import PyDrive and associated libraries.
# This only needs to be done once per notebook.
from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive
from google.colab import auth
from oauth2client.client import GoogleCredentials
# Authenticate and create the PyDrive client.
# This only needs to be done once per notebook.
auth.authenticate_user()
gauth = GoogleAuth()
gauth.credentials = GoogleCredentials.get_application_default()
drive = GoogleDrive(gauth)
# List .txt files in the root.
#
# Search query reference:
# https://developers.google.com/drive/v2/web/search-parameters
listed = drive.ListFile({'q': "title contains 'CV'"}).GetList()
for file in listed:
print('title {}, id {}'.format(file['title'], file['id']))