上传大文件不起作用 - Google Drive Python API

问题描述:

此脚本适用于小文件,但不适用于我尝试上传大文件 (250MB) 时.当我手动将相同的大文件上传到 GD 时,只需不到 10 秒,所以我认为我的连接不是问题.

This script works for small files, but not when I try to upload a large file (250MB). When I manually upload the same large file to GD it takes less than 10 seconds, so I assume my connection is not the problem.

upload.py

from __future__ import print_function
import os
import sys

from apiclient.http import MediaFileUpload
from apiclient.discovery import build
from httplib2 import Http
from oauth2client import file, client, tools

try:
    import argparse
    flags = argparse.ArgumentParser(parents=[tools.argparser]).parse_args()
except ImportError:
    flags = None

SCOPES = 'https://www.googleapis.com/auth/drive.file'
store = file.Storage(r'C:\Users\lucas.rezende\.credentials\storage.json')
creds = store.get()

if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets(r'C:\Users\lucas.rezende\.credentials\client_secret.json', scope=SCOPES)
    creds = tools.run_flow(flow, store, flags) if flags else tools.run(flow, store)
DRIVE = build('drive', 'v3', http=creds.authorize(Http()))

FILES = (
    ('OfertasMensais_20170418_n.xlsx', 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
)

for filename, mimeType in FILES:

    media_body = MediaFileUpload(filename, chunksize=1024*256, resumable = True)

    folder_id = '0000'
    metadata = {'name': filename, 'parents': [folder_id]}

    if mimeType:
        metadata['mimeType'] = mimeType
    res = DRIVE.files().create(body=metadata, media_body=filename).execute()

    if res:
        print('Uploaded "%s" (%s)' % (filename, res['mimeType']))

当我运行 python uploadfile.py cmd 屏幕永远保持这样:

When I run python uploadfile.py cmd screen stays like that eternally:

有人可以帮助发现如何进行这项工作吗?我不是专业的程序员,我在这方面花了将近两个小时试图使这项工作成功.

Can someone help to discover how to make this work? I am not a professional programmer and I am stuck into this for almost two hours trying to make this work.

v3 的解决方案是使用分块方法,但使用 create() 函数而不是 insert()

The solution for this with v3 is to use the chunked approach, but with the create() function rather than insert()

            res = None
            media_body = MediaFileUpload(filename, chunksize=1024*256, resumable = True)

            drive_request = self.drive.files().create(body=metadata,media_body=media_body)
            while res is None:
                status, res = drive_request.next_chunk()