I am trying to upload a video to YouTube using the Java API like this:
private Video uploadVideo(final YouTube youtube, final Video video, final InputStreamContent mediaContent)
throws IOException {
YouTube.Videos.Insert videoInsert = youtube.videos().insert("snippet,statistics,status", video, mediaContent);
MediaHttpUploader uploader = videoInsert.getMediaHttpUploader();
uploader.setDirectUploadEnabled(false);
uploader.setProgressListener(progressListener);
return videoInsert.execute();
}
}
I have a slow and instable internet connection and it would be really helpful if I could get resumable uploads to work. The documentation here and here seem to suggest to me that the "setDirectUploadEnabled(false)" should completely do the trick. Well... obviously it does not.
If my internet connection is interrupted an IOException is thrown and there is no automatic upload resume.
How can I resume an upload?
The documentation seems to lag behind the actual implementation. For luck it is open source and you can simply read the source code to find the solution. This is about version 1.16-rc:
If an IOException occurs during uploading, the
com.google.api.client.googleapis.media.MediaUploadErrorHandler
is used to handle the exception. This handler also takes care of resuming an upload, but it only does so if yet another exception handler registered at the HTTP-Request successfully handles the IOException. This precondition may change in the future as a comment in the current version indicates:Since the default io exception handler is null one must explicitly be set. This can be done when building the
YouTube
-Object:The example source code here initializes the
YouTube
object like this:To get this example to work, modify the line to read:
The
HTTPRequestInitializer
will be called every time aHTTPRequest
is constructed and sets the IOExceptionHandler. From there on theMediaUploadErrorHandler
will first pass the Exception to theHttpBackOffIOExceptionHandler
which may sleep for a while before allowing a retry. After that theMediaUploadErrorHandler
will try to continue the upload.Note that
credential
is anHttpRequestInitializer
as well and you need both initializers to execute. Thus in my example,credential
is called within the Initializer that I added.