Reputation: 64739
How do you diagnose and fix Google's unresponsive Drive API?
I'm trying to upload a several small files, and sometimes it runs perfectly and only takes a few seconds. Other times it seems to hang indefinitely. I've left it running for an hour trying to upload a tiny 1MB file. I'm not sure how to fix it since it's not throwing any exception, the permissions seem to be fine, and my Internet connection seems to be more than fast enough. It's simply taking forever, and feels like something behind the scenes has timed out.
This is my code:
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()
Upvotes: 4
Views: 1257
Reputation: 22296
Drive uploads are slow. The recommendation is to use resumable uploads and set the smallest chunk size (256k). It might be worth digging into the library you're using to configure chunk size and then report progress after each chunk.
Upvotes: 4