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?
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.
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.
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.
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With