【问题标题】:Downloading files from Google Drive with Python使用 Python 从 Google Drive 下载文件
【发布时间】:2020-08-09 21:08:59
【问题描述】:

我正在使用 Python 进行编码,以制作一个使用 Googl Drive API 下载文件的程序。 这是代码:

import os
import io
from googleapiclient.discovery import build
from googleapiclient.http import MediaIOBaseDownload

CLIENT_SECRET_FILE = 'client_secret.json'
API_NAME = 'drive'
API_VERSION = 'v3'
SCOPES = ['https://www.googleapis.com/auth/drive']

def main():
    """Shows basic usage of the Drive v3 API.
    Prints the names and ids of the first 10 files the user has access to.
    """
    

    creds = None
    # The file token.pickle stores the user's access and refresh tokens, and is
    # created automatically when the authorization flow completes for the first
    # time.
    if os.path.exists('token.pickle'):
        with open('token.pickle', 'rb') as token:
            creds = pickle.load(token)
    # If there are no (valid) credentials available, let the user log in.
    if not creds or not creds.valid:
        if creds and creds.expired and creds.refresh_token:
            creds.refresh(Request())
        else:
            flow = InstalledAppFlow.from_client_secrets_file(
                'credentials.json', SCOPES)
            creds = flow.run_local_server(port=0)
        # Save the credentials for the next run
        with open('token.pickle', 'wb') as token:
            pickle.dump(creds, token)

    service = build('drive', 'v3', credentials=creds)



    file_ids = ['1aKiVaINA2-_o8zttzYiH60jShzFQRUny', '1Xyy9-LWwTtnWZ1HLVJ74XT0PVsoixHOi']
    file_names = ['PicsArt_08-02-06.15.34.jpg', 'PicsArt_07-22-04.49.32.jpg']
    for file_id, file_name in zip(file_ids, file_names):
        request = service.files().get_media(fileId=file_id)

        fh = io.BytesIO()
        downloader = MediaIOBaseDownload(fd=fh, request=request)

        done = False

        while not done:
            status, done = downloader.next_chunk()
            print('Download progress {0}'.format(status.progress( * 100)))
    
        fh.seek(0)
        with open(os.path.join('./Random Files', file_name), 'wb') as f:
            f.write(fh.read())
            f.close()

if __name__ == '__main__':
    main()

当我运行程序时,我收到了这个错误:

Traceback(最近一次调用最后一次): 文件“download.py”,第 4 行,在 从 googleapiclient.http 导入 MediaIOBaseDownload ImportError:无法从“googleapiclient.http”导入名称“MediaIOBaseDownload”(C:\Users\Usuario\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.8_qbz5n2kfra8p0\LocalCache\local-packages\Python38\site-packages\googleapiclient\http .py)

有什么办法吗?

【问题讨论】:

  • 阅读此内容 - codelabs.developers.google.com/codelabs/gsuite-apis-intro/#4 - 在您的终端 python3 -c "import googleapiclient, httplib2, oauth2client" 中运行此命令(如有必要,将 python3 替换为 python)。你得到了哪些错误?
  • Traceback(最近一次调用最后一次):文件“”,第 1 行,在 ModuleNotFoundError: No module named 'oauth2client' 我已经用 pip 安装了 oauth2client,但错误仍然存​​在
  • 我已经安装了 oauth2client(如前所述),现在当我运行 python3 -c "import googleapiclient, httplib2, oauth2client" 时,我没有收到错误。但是当我运行我的程序时,导入错误仍然存​​在

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


【解决方案1】:

我知道您的目标是从 Python googleapiclient package 导入类 http.MediaIoBaseDownload。如果我理解正确,那么您就非常接近实现目标了。你应该用这个来改变有问题的行:

from googleapiclient.http import MediaIoBaseDownload

请注意MediaIoBaseDownload 的拼写方式不同。如果您仍有疑问,可以问我任何问题。

【讨论】:

    猜你喜欢
    • 2021-11-17
    • 1970-01-01
    • 2013-09-04
    • 2020-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-06
    相关资源
    最近更新 更多