Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 255
Martin Avatar asked Jun 26 '12 11:06

Martin


People also ask

How do I transfer large files to Google Drive?

Drag files into Google Drive On your computer, go to drive.google.com. Open or create a folder. To upload files and folders, drag them into the Google Drive folder.

Can I upload 5 GB file in Google Drive?

Drive upload limitsIndividual users can only upload 750 GB each day between My Drive and all shared drives. Users who reach the 750-GB limit or upload a file larger than 750 GB cannot upload additional files that day. Uploads that are in progress will complete.

How do I upload 1gb files to Google Drive?

Tip: There will be a Google Drive disk on your computer once you set up your Google Drive for Desktop. If you want to upload your large files to Google Drive manually, go to the Google Drive disk and drag your large files to the folder in it directly.


1 Answers

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:

  • com.google.api.services.drive.Drive.Files.Insert
  • com.google.api.client.googleapis.media.MediaHttpUploader
like image 94
Vic Fryzel Avatar answered Sep 28 '22 14:09

Vic Fryzel