【问题标题】:Downloading files in Google Drive folder to Local C-Drive through Python通过 Python 将 Google Drive 文件夹中的文件下载到本地 C-Drive
【发布时间】:2018-11-02 15:31:30
【问题描述】:

我正在尝试下载一个 .csv 文件,该文件通过 Microsoft Flow 的过程填充到 Google Drive 文件夹中,该过程每 6 小时通过电子邮件检索一个电子邮件附件文档。我试图遵循使用 Pydrive 模块的文档。我知道如何创建文件以上传和下载到 Google Drive,但不知道如何根据现有文件的链接 URL 下载。这是我的代码。

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

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

file_obj = drive.CreateFile({'id': '1j2vJVOB-_Xltmp9xkQJZzGb-5DAB_Imu'})
print(file_obj["title"], file_obj["mimeType"])
file_obj.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')

我收到的错误消息是。

    runfile('C:/Google_Python_Test/Google/Google_Drive1.py', wdir='C:/Google_Python_Test/Google')
Your browser has been opened to visit:

    https://accounts.google.com/o/oauth2/auth?client_id=1078392182164-kone7oddogt31qfcg1qvp5u13fc3tivi.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2F&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive&access_type=offline&response_type=code

Authentication successful.
Overhead application/vnd.google-apps.folder
Traceback (most recent call last):

  File "<ipython-input-26-6344c96b3f6e>", line 1, in <module>
    runfile('C:/Google_Python_Test/Google/Google_Drive1.py', wdir='C:/Google_Python_Test/Google')

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Google_Python_Test/Google/Google_Drive1.py", line 11, in <module>
    file_obj.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\pydrive\files.py", line 210, in GetContentFile
    self.FetchContent(mimetype, remove_bom)

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\pydrive\files.py", line 43, in _decorated
    return decoratee(self, *args, **kwargs)

  File "C:\Users\shrevee\Anaconda3\lib\site-packages\pydrive\files.py", line 265, in FetchContent
    'No downloadLink/exportLinks for mimetype found in metadata')

FileNotDownloadableError: No downloadLink/exportLinks for mimetype found in metadata

【问题讨论】:

  • 您确定文件是 csv 并且 id 正确吗?还是该文件是 Google mime 类型的?
  • 是的,文件是 csv,以及正确的文件 ID。
  • @EricShreve 如打印输出所示,给出的文件 ID 实际上不正确,它是文件夹“开销”的 ID,这就是您收到错误的原因。

标签: python python-3.x google-drive-api pydrive google-python-api


【解决方案1】:

您可能需要为文件指定MIME 类型,以确保它可以正确下载。如果您绝对确定该文件是 csv 并且未转换为 Google 文档,那么您可以这样做:

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

gauth = GoogleAuth()
gauth.LocalWebserverAuth()

drive = GoogleDrive(gauth)

file_obj = drive.CreateFile({'id': '1j2vJVOB-_Xltmp9xkQJZzGb-5DAB_Imu'})
file_obj.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')

【讨论】:

  • Karl,当我尝试执行该脚本时,我收到了FileNotDownloadableError: No downloadLink/exportLinks for mimetype found in metadata
  • 你试过text/csv作为mimetype吗?我认为这实际上是基于与下载 CSV 相关的this github 问题的正确方法。另外,您可以在执行drive.CreateFile() 之后执行print(file_obj["title"], file_obj["mimeType"]) 并显示输出是什么吗?这样我们就可以验证文件存储在 GDrive 中的 MIME 类型
  • 在关注此file = drive.CreateFile({'id': '1j2vJVOB-_Xltmp9xkQJZzGb-5DAB_Imu'}) print(file_obj["title"], file_obj["mimeType"]) file.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv') 之后,我仍然收到 No downloadLink/exportLinks 错误
  • 但是打印出来的是什么?这就是 print 声明的目的。它将输出文件名和mimeType,可用于找出问题所在。
  • 添加代码后,我在控制台中收到了这个。 file_obj = drive.CreateFile({'id': '1j2vJVOB-_Xltmp9xkQJZzGb-5DAB_Imu'}) print(file_obj["title"], file_obj["mimeType"]) file_obj.GetContentFile('Assigned_Agency_AZS_Overhead_EXCEL.csv', mimetype='text/csv')我没有得到打印结果。
猜你喜欢
  • 1970-01-01
  • 2013-06-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多