問題描述
我遇到了與 這個和這個 但在 Java 域中.這個問題也涵蓋了我想要的,但由于沒有答案,我想我會在這里問它,并提供更多細(xì)節(jié).
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 應(yīng)用程序來將文件備份到 Google Drive.正如其他人發(fā)現(xiàn)的那樣,內(nèi)部服務(wù)器錯誤 500"失敗是上傳的一個非常常見的問題,但對于小文件,實現(xiàn) 推薦的指數(shù)退避和重試 工作正常.然而,對于大文件(任何超過幾 MB 的文件),失敗率高得令人無法接受.在某些情況下,我的故障率遠(yuǎn)遠(yuǎn)超過 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 插入(上傳)文件時,文檔明確說明 可以使用三種上傳類型:簡單、多部分和可恢復(fù).通過向端點 URL 添加參數(shù)來指定上傳類型.顯然,我追求的是可恢復(fù)上傳類型.
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 設(shè)置此 up??loadType 參數(shù)的方法.有一個方法調(diào)用來設(shè)置每個可選參數(shù)(詳見這里),但不是一種將uploadType設(shè)置為可恢復(fù)的方法.沒有代碼片段,沒有文檔,什么都沒有.
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.
有點令人困惑的是,還有一個似乎是不相關(guān)的分塊"媒體上傳模式,這是默認(rèn)設(shè)置,我實際上通過調(diào)用 request.getMediaHttpUploader().setDirectUploadEnabled(true) 在我的應(yīng)用程序中禁用了它)
,因為它似乎對上傳的可靠性沒有任何影響,無論塊大小設(shè)置為多少,它都會大大減慢上傳速度.
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 并手動構(gòu)建請求,但我真的很想知道是否有其他人首先遇到/解決了這個問題.這是一個如此明顯的遺漏,我不敢相信很多人以前沒有遇到過.
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.
大家干杯.
大衛(wèi).
推薦答案
簡答:
當(dāng)您使用 Google Drive java 客戶端 API 時,傳遞給 MediaHttpUploader 的可恢復(fù)上傳的 URI 似乎與 Google Drive API V2.所以實際上默認(rèn)情況下,Google Drive Java 客戶端 API 已經(jīng)在使用可恢復(fù)上傳.
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
您會發(fā)現(xiàn)Drive的構(gòu)造函數(shù)將URI傳遞給DriveRequest,它也將變量uriTemplate傳遞給AbstractGoogleJsonClientRequest強(qiáng)>.最后,AbstractGoogleJsonClientRequest 使用 buildHttpRequestUrl() 生成 Google Drive API 參考.此 URI 存儲在 AbstractGoogleJsonClientRequest 中的變量 httpRequestUrl 中.httpRequestUrl 然后將被傳遞給 上傳者的上傳方法.這 方法 默認(rèn)情況下(directUploadEnabled 默認(rèn)為 false)將使用可恢復(fù)上傳而不是直接上傳.
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.
這篇關(guān)于在 Google Drive Java API 中設(shè)置 uploadType的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!