通过Python将Google Drive文件夹中的文件下载到本地C-Drive

时间:2018-11-02 15:31:30

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

我正在尝试下载一个.csv文件,该文件通过Microsoft Flow的过程填充到Google云端硬盘文件夹中,该流程每6个小时通过电子邮件检索一次电子邮件附件文档。我试图遵循使用Pydrive模块的文档。我知道如何创建文件以上传并下载到Google云端硬盘,但不知道如何根据现有文件的链接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

1 个答案:

答案 0 :(得分:0)

您可能需要为文件指定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')