简体   繁体   English

如何使用 Python 在 Google Drive 上下载目录?

[英]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.我相信你的情况和目标如下。

  • By 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。
  • In your issue, when the folder is used in the loop, the error occurs.在您的问题中,当在循环中使用该文件夹时,会发生错误。
  • You want to remove this error.您想删除此错误。

For this, how about this answer?为此,这个答案怎么样?

Modification point:修改点:

  • When the mimeType is included in items of items = self.listFilesInFolder(downLoadFolderKey) , the folder can be checked by the mimeType.当 mimeType 包含在items items = self.listFilesInFolder(downLoadFolderKey)的项目中时,可以通过 mimeType 检查文件夹。 The mimeType of folder is application/vnd.google-apps.folder .文件夹的 mimeType 是application/vnd.google-apps.folder
  • From your script, I think that when the Google Docs file (Spreadsheet, Document, Slides and so on) is downloaded with the method of "Files: get", the same error occurs.从您的脚本中,我认为当使用“文件:get”的方法下载 Google Docs 文件(电子表格、文档、幻灯片等)时,会发生同样的错误。
  • In order to download the Google Docs files, it is required to use the method of "Files: export".要下载 Google Docs 文件,需要使用“文件:导出”的方法。

When above point is reflected to your script, how about the following modification?当上述观点反映到您的脚本中时,以下修改如何?

Modified script:修改后的脚本:

From: 从:
 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)
  • In this modification, at first, please confirm whether the file mimeType to 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 的值下载 Google Docs 文件和 Google Docs 以外的文件。
  • In this modification, as a sample modification, Google Docs files are downloaded as the PDF file.在此修改中,作为示例修改,Google Docs 文件被下载为 PDF 文件。 If you want to change the output mimeType, please modify mimeType='application/pdf' .如果要更改 output mimeType,请修改mimeType='application/pdf'

References:参考:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM