我的 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[]。
有任何想法吗?