Python 谷歌驱动API上传超时
您如何诊断和修复Google的无响应驱动器API 我试着上传一些小文件,有时它运行得很好,只需要几秒钟。其他时候,它似乎无限期地挂着。我让它运行了一个小时,试图上传一个1MB的小文件。我不知道如何修复它,因为它没有抛出任何异常,权限似乎很好,我的互联网连接似乎足够快。这简直要花上一辈子的时间,感觉幕后的事情已经超时了 这是我的代码:Python 谷歌驱动API上传超时,python,google-drive-api,Python,Google Drive Api,您如何诊断和修复Google的无响应驱动器API 我试着上传一些小文件,有时它运行得很好,只需要几秒钟。其他时候,它似乎无限期地挂着。我让它运行了一个小时,试图上传一个1MB的小文件。我不知道如何修复它,因为它没有抛出任何异常,权限似乎很好,我的互联网连接似乎足够快。这简直要花上一辈子的时间,感觉幕后的事情已经超时了 这是我的代码: import httplib2 import os from apiclient import discovery from apiclient.http imp
import httplib2
import os
from apiclient import discovery
from apiclient.http import MediaFileUpload
import oauth2client
from oauth2client import client
from oauth2client import tools
SCOPES = 'https://www.googleapis.com/auth/drive'
CLIENT_SECRET_FILE = 'my-app-secret-file.json'
APPLICATION_NAME = 'my-app'
def get_installed_app_credentials():
"""Gets valid user credentials from storage.
If nothing has been stored, or if the stored credentials are invalid,
the OAuth2 flow is completed to obtain the new credentials.
Returns:
Credentials, the obtained credential.
"""
home_dir = os.path.expanduser('~')
credential_dir = os.path.join(home_dir, '.credentials')
if not os.path.exists(credential_dir):
os.makedirs(credential_dir)
credential_path = os.path.join(credential_dir,
'drive-quickstart.json')
store = oauth2client.file.Storage(credential_path)
credentials = store.get()
if not credentials or credentials.invalid:
flow = client.flow_from_clientsecrets(CLIENT_SECRET_FILE, SCOPES)
flow.user_agent = APPLICATION_NAME
credentials = tools.run(flow, store)
print 'Storing credentials to ' + credential_path
return credentials
def upload_file():
credentials = get_installed_app_credentials()
http = credentials.authorize(httplib2.Http())
service = discovery.build('drive', 'v2', http=http)
drive_folder_id = '0wlekrjelkjsfdBHYjdhb21CNm5'
filename = 'myfile.txt'
file = service.files().insert(
body={
'title': filename,
'parents': [{'id': drive_folder_id}],
},
media_body=MediaFileUpload(
filename,
resumable=True),
).execute()
驱动器上传速度慢。建议使用可恢复上传,并设置最小块大小(256k)。可能值得深入研究您用来配置块大小的库,然后在每个块之后报告进度。这是最好的方法,因为如果请求花费的时间太长,您可以中止请求,然后查询以查看有多少数据(如果有)已由驱动器上载服务器接收。这有助于我解决大型文件(>100MB)的写入操作超时错误