I am using the Java client library for Google Drive API to upload some text files and convert them to Google doc format. The code runs on Google App Engine. The main code segment looks like this:
File fileMetadata = new File();
fileMetadata.setTitle("Document title");
fileMetadata.setDescription("Desc goes here");
fileMetadata.setMimeType("text/plain; charset=utf-8");
ByteArrayContent byteArrayContent = ByteArrayContent.fromString(
"text/plain; charset=utf-8", "Text file's content goes here");
Drive.Files.Insert insertRequest = driveService.files()
.insert(fileMetadata, byteArrayContent).setConvert(true);
File insertedFile = insertRequest.execute();
For small text files, the above code works fine. But when the text files are big (perhaps there are some other factors that I am not aware of), it would throw the following exception:
Uncaught exception from servlet
java.net.SocketTimeoutException: Timeout while fetching URL: https://www.googleapis.com/upload/drive/v2/files?convert=true&uploadType=resumable&upload_id=AEnB2UpPDz6jvM8zO2zPFxFHmoCiisplOf1Ui5fngZdI4qqoK6hwt_wtOt89RcW3QautW9FPlHKfMznYA4gmo95qdWthJQgWpQ
at com.google.appengine.api.urlfetch.URLFetchServiceImpl.convertApplicationException(URLFetchServiceImpl.java:145)
at com.google.appengine.api.urlfetch.URLFetchServiceImpl.fetch(URLFetchServiceImpl.java:45)
at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.fetchResponse(URLFetchServiceStreamHandler.java:419)
at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getInputStream(URLFetchServiceStreamHandler.java:298)
at com.google.apphosting.utils.security.urlfetch.URLFetchServiceStreamHandler$Connection.getResponseCode(URLFetchServiceStreamHandler.java:151)
at com.google.api.client.http.javanet.NetHttpResponse.<init>(NetHttpResponse.java:36)
at com.google.api.client.http.javanet.NetHttpRequest.execute(NetHttpRequest.java:94)
at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:965)
at com.google.api.client.googleapis.media.MediaHttpUploader.executeCurrentRequestWithoutGZip(MediaHttpUploader.java:545)
at com.google.api.client.googleapis.media.MediaHttpUploader.resumableUpload(MediaHttpUploader.java:417)
at com.google.api.client.googleapis.media.MediaHttpUploader.upload(MediaHttpUploader.java:336)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:418)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:343)
at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:460)
Some people said it's due to the URLConnnection's timeout. But I am using the Java client library, which doesn't allow me to set the timeout explicitly. Also, the above exception happens at least 30 seconds after the insertRequest.execute() call.
I suspect the Java client library has set an internal timeout of 30 seconds... I hope some Google software engineers can take a look.
Any idea will be appreciated. Thanks very much.
This error is expected when you are uploading a big file. Try implementing a resumable upload. You can read more about the method as well as a sample in the following page: https://developers.google.com/drive/web/manage-uploads#resumable
Based on the Java library, you should add the following code before the insertRequest.execute()
MediaHttpUploader uploader = insert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(useDirectUpload);
uploader.setProgressListener(new FileUploadProgressListener());