【问题标题】:PyDrive download file from a SHARED drivePyDrive 从共享驱动器下载文件
【发布时间】:2019-11-18 13:21:21
【问题描述】:

我知道如何使用 PyDrive 从我的驱动器下载文件,问题是我需要在共享驱动器上下载(或至少打开)一个 xlsx 文件。这是我到目前为止下载文件的代码:

from pydrive.auth import GoogleAuth
from pydrive.drive import GoogleDrive

gauth = GoogleAuth()
gauth.LoadClientConfigFile('client_secret.json')
drive = GoogleDrive(gauth)

team_drive_id = 'XXXXX'
parent_folder_id = 'XXXXX'
file_id = 'XXXXX'

f = drive.CreateFile({
    'id': file_id,
    'parents': [{
        'kind': 'drive#fileLink',
        'teamDriveId': team_drive_id,
        'id': parent_folder_id
    }]
})
f.GetContentFile('<file_name>')

代码返回错误 404(找不到文件),这是有道理的:当我检查 GetContentFile 正在查看的 URL 时,它是指向我的驱动器的 URL,而不是共享驱动器。我可能错过了“supportsTeamDrives”:在某处是真的(但在哪里?)。

实际上在https://github.com/gsuitedevs/PyDrive/issues/149 上有一个与我的问题相关的帖子,其中有人提出了完全相同的问题。显然,这让开发人员在大约两周前修改了 PyDrive,但我仍然不明白如何解释他的修改以及如何解决我的问题。我没有注意到 Stack Overflow 上的任何其他类似帖子(无论如何都不是关于从共享驱动器下载)。任何帮助将不胜感激。

亲切的问候, 贝尔蒂

【问题讨论】:

    标签: pydrive


    【解决方案1】:

    我在较新的 Github 帖子中找到了答案:https://github.com/gsuitedevs/PyDrive/issues/160

    来自 SMB784 的回答有效:“supportsTeamDrives=True”应添加到第 235-6 行的 files.py(pydrive 包)中。

    这绝对解决了我的问题。

    【讨论】:

      【解决方案2】:

      pydrive 移动到pydrive2

      遇到同样的问题后,我在an issue within the googleworkspace/PyDrive GitHub page from 2020上看到了这条评论:

      ...DVC.org 团队有一个积极维护的版本 PyDrive2(Travis 测试,定期发布,包括 conda)...请试一试并告诉我们 - https://github.com/iterative/PyDrive2

      pydrive 包移动到pydrive2 包使我能够下载存储在shared drive 上的文件的本地副本只需要我知道文件ID

      安装pydrive2后,您可以使用以下代码模板在共享驱动器中下载文件的本地副本:

      # load necessary modules ----
      from pydrive2.auth import GoogleAuth
      from pydrive2.drive import GoogleDrive
      
      # authenticate yourself using your credentials
      gauth = GoogleAuth()
      gauth.LoadClientConfigFile('client_secret.json')
      drive = GoogleDrive(gauth)
      
      # store the file ID of the file located within the shared drive
      file_id = 'XXXXX'
      
      # store the output file name
      output_file_name = 'YYYYY'
      
      # create an instance of Google Drive file with auth of this instance
      f = drive.CreateFile({'id': file_id})
      
      # save content of this file as a local file
      f.GetContentFile(output_file_name)
      

      【讨论】:

      • 谢谢你,这样生活更安全!
      猜你喜欢
      • 1970-01-01
      • 2021-04-05
      • 1970-01-01
      • 1970-01-01
      • 2018-07-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      相关资源
      最近更新 更多