2012-06-26 24 views
7

我的BLOB存儲在我的Blobstore中,並且希望將這些文件推送到Google Drive。 當我使用谷歌應用程序引擎UrlFetchService如何將大型文件(> 5 MB)從Blobstore發佈到Google Drive?

URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService(); 
URL url = new URL("https://www.googleapis.com/upload/drive/v1/files"); 
HTTPRequest httpRequest = new HTTPRequest(url, HTTPMethod.POST); 
httpRequest.addHeader(new HTTPHeader("Content-Type", contentType)); 
httpRequest.addHeader(new HTTPHeader("Authorization", "OAuth " + accessToken)); 
httpRequest.setPayload(buffer.array()); 
Future<HTTPResponse> future = fetcher.fetchAsync(httpRequest); 
try { 
    HTTPResponse response = (HTTPResponse) future.get(); 
} catch (Exception e) { 
    log.warning(e.getMessage()); 
} 

問題:當文件超過5 MB,它超過了UrlFetchService請求大小的限制(鏈接:https://developers.google.com/appengine/docs/java/urlfetch/overview#Quotas_and_Limits

備選:使用谷歌在雲端硬盤API我有這樣的代碼:

File body = new File(); 
body.setTitle(title); 
body.setDescription(description); 
body.setMimeType(mimeType); 

// File's content. 
java.io.File fileContent = new java.io.File(filename); 
FileContent mediaContent = new FileContent(mimeType, fileContent); 

File file = service.files().insert(body, mediaContent).execute(); 

問題與此解決方案: Google App Engine不支持FileOutputStream來管理從Blobstore讀取的byte []。

任何想法?

回答

6

要做到這一點,請使用小於5兆字節塊的可恢復上載。在Google雲端硬盤的Java API客戶端中執行此操作非常簡單。以下是您已經提供的Drive代碼改編的代碼示例。

File body = new File(); 
body.setTitle(title); 
body.setDescription(description); 
body.setMimeType(mimeType); 

java.io.File fileContent = new java.io.File(filename); 
FileContent mediaContent = new FileContent(mimeType, fileContent); 

Drive.Files.Insert insert = drive.files().insert(body, mediaContent); 
insert.getMediaHttpUploader().setChunkSize(1024 * 1024); 
File file = insert.execute(); 

欲瞭解更多信息,請參閱javadoc,瞭解相關的類:

+0

感謝適應我的驅動器代碼。我通過利用可恢復的上傳來更新我的應用程序。 將此代碼上傳到Google App Engine後,我得到 java.lang.NoSuchMethodError:com.google.api.services.drive.Drive $ Files $ Insert.getMediaHttpUploader()Lcom/google/api/client/googleapis/MediaHttpUploader; 我需要多研究一下這個...... – Martin

+0

你有哪個版本的google-api-java-client?這是最新的嗎? 請看這裏: http://code.google.com/p/google-api-java-client/ –

+0

謝謝Vic!更新所有使用的API庫後,您提供的代碼完美無缺! – Martin

相關問題