在 Google Drive Java API 中设置 uploadType

我遇到了与 这个和这个 但在 Java 域中.这个问题也涵盖了我想要的,但由于没有答案,我想我会在这里问它,并提供更多细节.

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.

我主要是通过编写 Java 应用程序来将文件备份到 Google Drive.正如其他人发现的那样,内部服务器错误 500"失败是上传的一个非常常见的问题,但对于小文件,实现 推荐的指数退避和重试 工作正常.然而,对于大文件(任何超过几 MB 的文件),失败率高得令人无法接受.在某些情况下,我的故障率远远超过 50%,这使得任何长时间的备份工作实际上都变得不可能.

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.

使用 Google Drive v2 API 插入(上传)文件时,文档明确说明 可以使用三种上传类型:简单、多部分和可恢复.通过向端点 URL 添加参数来指定上传类型.显然,我追求的是可恢复上传类型.

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.

似乎没有使用 API 设置此 up​​loadType 参数的方法.有一个方法调用来设置每个可选参数(详见这里),但不是一种将uploadType设置为可恢复的方法.没有代码片段,没有文档,什么都没有.

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.

有点令人困惑的是,还有一个似乎是不相关的分块"媒体上传模式,这是默认设置,我实际上通过调用 request.getMediaHttpUploader().setDirectUploadEnabled(true) 在我的应用程序中禁用了它),因为它似乎对上传的可靠性没有任何影响,无论块大小设置为多少,它都会大大减慢上传速度.

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.

我即将绕过 API 并手动构建请求,但我真的很想知道是否有其他人首先遇到/解决了这个问题.这是一个如此明显的遗漏,我不敢相信很多人以前没有遇到过.

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.

大家干杯.

大卫.

推荐答案

简答:

当您使用 Google Drive java 客户端 API 时,传递给 MediaHttpUploader 的可恢复上传的 URI 似乎与 Google Drive API V2.所以实际上默认情况下,Google Drive Java 客户端 API 已经在使用可恢复上传.

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.

在 Google Drive Java API 客户端中一路跟踪代码后

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

您会发现Drive的构造函数将URI传递给DriveRequest,它也将变量uriTemplate传递给AbstractGoogleJsonClientRequest.最后,AbstractGoogleJsonClientRequest 使用 buildHttpRequestUrl() 生成 Google Drive API 参考.此 URI 存储在 AbstractGoogleJsonClientRequest 中的变量 httpRequestUrl 中.httpRequestUrl 然后将被传递给 上传者的上传方法.这 方法 默认情况下(directUploadEnabled 默认为 false)将使用可恢复上传而不是直接上传.

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.

相关文章