Martin
Martin

Reputation: 103

How to POST a large file (> 5 mb) from Blobstore to Google Drive?

I have blobs stored in my Blobstore and want to push these files to the Google Drive. When I use the 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());
}

Problem: When the file exceeds 5 mb, it exceeds the limit of the UrlFetchService request size (Link: https://developers.google.com/appengine/docs/java/urlfetch/overview#Quotas_and_Limits)

Alternative: Using the Google Drive API I have this code:

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();

Problem with this solution: FileOutputStream is not supported on Google App Engine to manage byte[] read from Blobstore.

Any ideas?

Upvotes: 7

Views: 1575

Answers (1)

Vic Fryzel
Vic Fryzel

Reputation: 2395

To do this, use resumable upload with chunks smaller than 5 megabytes. This is simple to do in the Google API Java Client for Drive. Here is a code sample adapted from the Drive code you already provided.

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();

For more information, see the javadocs for the relevant classes:

Upvotes: 6

Related Questions