上载大文件不起作用-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()