4

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.

Community
  • 1
  • 1
davidf2281
  • 1,319
  • 12
  • 20

2 Answers2

3

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.

hencrice
  • 167
  • 1
  • 1
  • 10
2

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.

pinoyyid
  • 21,499
  • 14
  • 64
  • 115
  • Yes, that's for the chunked resumable *media* upload that I mentioned in my question (and which I've disabled because it doesn't appear to work). As you say, that source isn't related to the Drive API. The whole thing is a confused mess. – davidf2281 Oct 14 '13 at 14:34
  • I find myself going round in circles when navigating the documentation, frequently ending up on pages that relate to the old APIs. It's not a great deal of fun. – davidf2281 Oct 14 '13 at 14:37
  • The devs are rather fond of the Builder pattern, so my guess is that your control over the http uploader is done at service build time, rather than being a parameter to the upload method. I'm guessing a bit as I stopped trying to use the libs some time ago. – pinoyyid Oct 14 '13 at 15:24