在Google云端硬盘Java API中设置uploadType [英] Setting uploadType in Google Drive Java API

查看:269
本文介绍了在Google云端硬盘Java API中设置uploadType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到的问题与,但在Java领域。 这个问题也涵盖了我想要的,但由于没有答案即将到来,我想我会问在这里,一点点细节。



我是通过编写一个Java应用程序将文件备份到Google Drive。正如其他人所发现的,内部服务器错误500失败是上传的一个相当常见的问题,但是对于小文件,执行建议指数退避和重试工作正常。但是,对于大文件(超过几MB),故障率是无法接受的。在某些情况下,我的失败率超过了50%,这使得任何长时间的备份工作都无法实现。



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



问题



似乎没有方法使用API​​设置此uploadType参数。有一个方法调用来设置每个可选参数(详细的这里) ,但没有一种方法来设置uploadType的可恢复性。没有代码片段,没有文档,没有任何东西。

有点混乱,也有一个似乎是不相关的分块媒体上传模式,这是默认的,我实际上通过调用 request.getMediaHttpUploader()。setDirectUploadEnabled(true)禁用了我的应用程序,因为它似乎对上传的可靠性没有任何影响,问题是什么chunksize设置,并大大减缓上传下降。

我正在绕过API和手动建立请求的边缘,但我会真的很想知道是否有其他人遇到/解决了这个问题。这是一个明显的遗漏,我不能相信很多民间以前没有遇到过。

欢呼所有。



David。

解决方案

简短回答:

b $ b

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

长答案



在Google云端硬盘Java API客户端中追踪 $ b

com.google.api.services.driveDrive
$ b - > com.google.api.services.drive.Drive.Drive

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



您会发现云端硬盘的构造函数将URI传递给 DriveRequest ,它也将变量 uriTemplate 传递给 AbstractGoogleJsonClientRequest 。最后 AbstractGoogleJsonClientRequest 使用 buildHttpRequestUrl()生成 Google Drive API参考。该URI存储在 AbstractGoogleJsonClientRequest 中的变量 httpRequestUrl 中。然后,httpRequestUrl 将传递给上传器的上传方法。此

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.

解决方案

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.

这篇关于在Google云端硬盘Java API中设置uploadType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆