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

1

7

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?

Neile answered 26/6, 2012 at 11:25 Comment(0)
S
6

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:

Septuagint answered 26/6, 2012 at 19:21 Comment(3)
Thanks for adapting my Drive code. I updates my application by leveraging the resumable upload. After uploading this code to the Google App Engine I get java.lang.NoSuchMethodError: com.google.api.services.drive.Drive$Files$Insert.getMediaHttpUploader()Lcom/google/api/client/googleapis/MediaHttpUploader; I need to research a bit more on this...Neile
Which version of google-api-java-client do you have? Is it the latest? See here: code.google.com/p/google-api-java-clientSeptuagint
Thanks Vic! After updating all used API libraries your provided code works perfectly fine!Neile

© 2022 - 2024 — McMap. All rights reserved.