7

我的 Blobstore 中存储了 Blob,并希望将这些文件推送到 Google Drive。当我使用 Google App Engine UrlFetchService

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 )

替代方案:使用 Google Drive 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[]。

有任何想法吗?

4

1 回答 1

6

为此,请使用小于 5 兆字节的块的可恢复上传。这在 Google API Java Client for Drive 中很容易做到。这是改编自您已经提供的 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();

有关更多信息,请参阅相关类的 javadocs:

于 2012-06-26T19:21:40.327 回答