【发布时间】:2017-09-17 12:42:49
【问题描述】:
此脚本适用于小文件,但不适用于我尝试上传大文件 (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 cmd 时,屏幕永远保持这种状态:
有人可以帮助发现如何进行这项工作吗?我不是专业的程序员,为了完成这项工作,我被困了将近两个小时。
【问题讨论】:
-
你从哪里得到那个 MIME 类型的? stackoverflow.com/a/36957953/130453
-
@corn3lius 我从这篇文章的第三个答案中得到:stackoverflow.com/questions/11894772/…,但实际上我不想转换。只需将我的 xlsx 文件上传到 GD。
-
你试过
application/vnd.ms-excel吗? -
@corn3lius 刚改了还是卡住了...
标签: python python-3.x api google-drive-api