这个脚本适用于小文件,但当我试图上传一个大文件(250MB)时就不行了。当我手动将同一个大文件上传到GD时,只需不到10秒,所以我假设我的连接不是问题所在。在
上传.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
命令时,屏幕始终保持不变:
有人能帮我发现如何使这个工作吗?我不是一个专业的程序员,我被困在这几乎两个小时,试图使这个工作。在
遵循chunked范式,您需要特别调用
next_chunk()
来继续上传。请看这里:https://developers.google.com/api-client-library/python/guide/media_upload#resumable-media-chunked-uploadv3的解决方案是使用chunked方法,但是使用create()函数而不是insert()
相关问题 更多 >
编程相关推荐