我有与这篇文章中提出的问题完全相同的问题: List files and folders in a google drive folder 我没有在 google drive rest api 文档中弄清楚如何获取 google 文件夹中的文件列表驾驶
原文由 javi padilla 发布,翻译遵循 CC BY-SA 4.0 许可协议
我有与这篇文章中提出的问题完全相同的问题: List files and folders in a google drive folder 我没有在 google drive rest api 文档中弄清楚如何获取 google 文件夹中的文件列表驾驶
原文由 javi padilla 发布,翻译遵循 CC BY-SA 4.0 许可协议
这是一个 hacky-yet-successful 解决方案。这实际上是从一个特定的 Google Drive 文件夹(在本例中是一个名为“thumbnails”的文件夹) 获取 所有文件。我需要从特定文件夹中获取(不仅仅是列出)所有文件并对它们执行图像调整,因此我使用了以下代码:
`# 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)
原文由 todbott 发布,翻译遵循 CC BY-SA 4.0 许可协议
4 回答4.5k 阅读✓ 已解决
1 回答3.2k 阅读✓ 已解决
4 回答3.8k 阅读✓ 已解决
3 回答2.2k 阅读✓ 已解决
1 回答4.4k 阅读✓ 已解决
1 回答3.9k 阅读✓ 已解决
1 回答2.8k 阅读✓ 已解决
您可以在此处查看有关如何在云端硬盘中列出文件的示例: https ://developers.google.com/drive/api/v3/search-files。您需要构建一个列出文件夹中文件的查询:使用
其中 1234 是您要列出的文件夹的 ID。您可以修改查询以列出特定类型的所有文件(例如文件夹中的所有 jpeg 文件)等。