[英]How to Download a directory on Google Drive using Python?
service = self.auth()
items = self.listFilesInFolder(downLoadFolderKey)
for item in items:
file_id = (item.get('id'))
file_name = (item.get('name'))
request = service.files().get_media(fileId=file_id)
fh = io.BytesIO()
downloader = MediaIoBaseDownload(fh, request)
done = False
while done is False:
status, done = downloader.next_chunk()
print ("Download %d%%." % int(status.progress() * 100) + file_name)
filepath = fileDownPath + file_name
with io.open(filepath, 'wb') as f:
fh.seek(0)
f.write(fh.read())
I am using Google Drive API v3.我正在使用 Google Drive API v3。 I am trying to download a full directory.
我正在尝试下载完整目录。 But the problem is the directory itself contains folders and when I try to run this bit of code.
但问题是目录本身包含文件夹,当我尝试运行这段代码时。 This error happens.
发生此错误。
<HttpError 403 when requesting https://www.googleapis.com/drive/v3/files/1ssF0XD8pi6oh6DXB1prIJPWKMz9dggm2?alt=media returned "Only files with binary content can be downloaded. Use Export with Google Docs files.">
The error I figure is due to it trying to download the folders, within the directory.我认为的错误是由于它试图下载目录中的文件夹。 But how do I download the full directory?
但是如何下载完整目录?
PS The directory changes so I cannot hard code file IDs and then download the files. PS 目录更改,因此我无法对文件 ID 进行硬编码,然后下载文件。
I believe your situation and goal as follows.我相信你的情况和目标如下。
items = self.listFilesInFolder(downLoadFolderKey)
, you have already been able to retrieve all file and folder list including the subfolders under the specific folder.items = self.listFilesInFolder(downLoadFolderKey)
,您已经能够检索所有文件和文件夹列表,包括特定文件夹下的子文件夹。items
include the mimeType for each files and folders. items
包括每个文件和文件夹的 mimeType。 For this, how about this answer?为此,这个答案怎么样?
items
of items = self.listFilesInFolder(downLoadFolderKey)
, the folder can be checked by the mimeType.items
items = self.listFilesInFolder(downLoadFolderKey)
的项目中时,可以通过 mimeType 检查文件夹。 The mimeType of folder is application/vnd.google-apps.folder
.application/vnd.google-apps.folder
。 When above point is reflected to your script, how about the following modification?当上述观点反映到您的脚本中时,以下修改如何?
request = service.files().get_media(fileId=file_id)
To:
file_mimeType = (item.get('mimeType')) if file_mimeType == 'application/vnd.google-apps.folder': continue request = service.files().export_media(fileId=file_id, mimeType='application/pdf') if 'application/vnd.google-apps' in file_mimeType else service.files().get_media(fileId=file_id)
items
of items = self.listFilesInFolder(downLoadFolderKey)
is included, again .items
items = self.listFilesInFolder(downLoadFolderKey)
项的文件 mimeType 。 By this, the folder can be skipped and also, Google Docs files and the files except for Google Docs can be downloaded using the value of mimeType.mimeType='application/pdf'
.mimeType='application/pdf'
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.