我的blob存储在blobstore中,并希望将这些文件推送到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 []。
有什么想法吗?
答案 0 :(得分:6)
要执行此操作,请使用小于5兆字节块的可恢复上载。这在Google API Java Client 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();
有关更多信息,请参阅相关类的javadoc: