Java 如何将大文件(>;5 mb)从Blobstore发布到Google Drive?
我的Blobstore中存储了Blob,希望将这些文件推送到Google驱动器。 当我使用谷歌应用程序引擎服务时Java 如何将大文件(>;5 mb)从Blobstore发布到Google Drive?,java,google-app-engine,blobstore,google-drive-api,Java,Google App Engine,Blobstore,Google Drive Api,我的Blobstore中存储了Blob,希望将这些文件推送到Google驱动器。 当我使用谷歌应用程序引擎服务时 URLFetchService fetcher = URLFetchServiceFactory.getURLFetchService(); URL url = new URL("https://www.googleapis.com/upload/drive/v1/files"); HTTPRequest httpRequest = new HTTPRequest(url, HTTP
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());
}
此解决方案的问题:Google App Engine不支持FileOutputStream来管理从Blobstore读取的字节[]
有什么想法吗?要做到这一点,请使用块小于5兆字节的可恢复上传。这在Google API Java客户端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:
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();