Question

I'm having the same problem as this and this but in the Java domain. This question also covers what I want but since no answers have been forthcoming I thought I'd ask it here, with a little more detail.

I'm most of the way through writing a Java application to back up files to Google Drive. As others have found, 'internal server error 500' failures are a pretty common problem with uploads, but for small files, implementing the recommended exponential back-off and retry works okay. For large files, however, (anything over a few MB) the failure rate is unacceptably high. In some cases I'm getting well over 50% failure rate, which makes any long backup job effectively impossible.

When inserting (uploading) a file using the Google Drive v2 API, the documentation clearly states that three upload types are available: simple, multipart and resumable. The upload type is specified by adding a parameter to the endpoint URL. Clearly, what I'm after is the resumable upload type.

The problem

There appears to be no method to set this uploadType parameter using the API. There's a method call to set every optional parameter (as detailed here), but not a sniff of a way to set uploadType to resumable. No code snippets, no documentation, no nothing.

Somewhat confusingly, there is also what appears to be an unrelated 'chunked' media upload mode, which is the default and which I've actually disabled in my application by calling request.getMediaHttpUploader().setDirectUploadEnabled(true), since it appears to make no difference whatsoever to the reliability of an upload, no matter what the chunksize is set to, and it hugely slows uploads down.

I'm on the verge of circumventing the API and building the requests manually, but I'd really like to know if anyone else has encountered/solved this first. It's such a glaring omission that I can't believe lots of folk haven't encountered it before.

Cheers all.

David.

Was it helpful?

Solution

Short answer:

The URI passed to MediaHttpUploader's resumable upload when you use the Google Drive java client API seems to be the same as the one proposed on Google Drive API V2. So actually by default the Google Drive Java client API is already using the resumable upload.

Long answer:

After tracing the code in Google Drive Java API client all the way from

com.google.api.services.drive.Drive

->com.google.api.services.drive.DriveRequest

->com.google.api.client.googleapis.services.AbstractGoogleJsonClientRequest

You will find that Drive's constructor passes the URI to DriveRequest, which also passes the variable uriTemplate to AbstractGoogleJsonClientRequest. And finally AbstractGoogleJsonClientRequest uses buildHttpRequestUrl() to generate the resumable URI proposed in Google Drive API reference. This URI is stored in the variable httpRequestUrl in AbstractGoogleJsonClientRequest. httpRequestUrl will then be passed to uploader's upload method. This method by default (directUploadEnabled defaults to false) will use the resumable upload instead of direct upload.

OTHER TIPS

There is an example on this page https://developers.google.com/gdata/docs/resumable_upload?csw=1#InitialRequestJava

Now, here's where it gets confusing. That page allegedly refers to the old style Gdata api. However the current source code at https://code.google.com/p/google-api-java-client/source/browse/google-api-client/src/main/java/com/google/api/client/googleapis/media/MediaHttpUploader.java references that page as being the API that it implements.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top