我在Blobstore中存储了Blob,并希望将这些文件推送到Google云端硬盘。
当我使用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 []。

有任何想法吗?

最佳答案

为此,请使用大小小于5 MB的断点续传。在Google API for 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:
  • com.google.api.services.drive.Drive.Files.Insert
  • com.google.api.client.googleapis.media.MediaHttpUploader
  • 07-24 09:45
    查看更多