Created
January 13, 2016 16:02
-
-
Save ramv-dailymotion/b72390ad6c89d2db3543 to your computer and use it in GitHub Desktop.
Resumable Upload of files to Google Cloud Storage
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential; | |
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport; | |
import com.google.api.client.googleapis.media.MediaHttpUploader; | |
import com.google.api.client.googleapis.media.MediaHttpUploaderProgressListener; | |
import com.google.api.client.http.*; | |
import com.google.api.client.json.JsonFactory; | |
import com.google.api.client.json.jackson2.JacksonFactory; | |
import com.google.api.client.googleapis.json.GoogleJsonResponseException; | |
import com.google.api.services.storage.Storage; | |
import com.google.api.services.storage.StorageScopes; | |
import com.google.api.services.storage.model.StorageObject; | |
import java.io.*; | |
import java.security.GeneralSecurityException; | |
public class FileUpload { | |
/** Global instance of the JSON factory. */ | |
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance(); | |
static class CustomProgressListener implements MediaHttpUploaderProgressListener { | |
public void progressChanged(MediaHttpUploader uploader) throws IOException { | |
switch (uploader.getUploadState()) { | |
case INITIATION_STARTED: | |
System.out.println("Initiation has started!"); | |
break; | |
case INITIATION_COMPLETE: | |
System.out.println("Initiation is complete!"); | |
break; | |
case MEDIA_IN_PROGRESS: | |
System.out.println("Media Upload in progress!"); | |
break; | |
case MEDIA_COMPLETE: | |
System.out.println("Upload is complete!"); | |
} | |
} | |
} | |
/** | |
* Uploads data to an object in a bucket. | |
* | |
* @param name the name of the destination object. | |
* @param contentType the MIME type of the data. | |
* @param stream the data - for instance, you can use a FileInputStream to upload a file. | |
* @throws Exception | |
*/ | |
public static void uploadStreamResumable(String name, | |
InputStream stream, | |
String contentType, | |
String bucket) | |
throws Exception{ | |
//File mediaFile = new File("/tmp/driveFile.jpg"); | |
InputStreamContent mediaContent = | |
new InputStreamContent(contentType, stream); | |
mediaContent.setLength(mediaContent.getLength()); | |
GoogleCredential credential = GoogleCredential.getApplicationDefault(); | |
// Depending on the environment that provides the default credentials (e.g. Compute Engine, | |
// App Engine), the credentials may require us to specify the scopes we need explicitly. | |
// Check for this case, and inject the Cloud Storage scope if required. | |
if (credential.createScopedRequired()) { | |
credential = credential.createScoped(StorageScopes.all()); | |
} | |
Storage client = getService(name); | |
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); | |
// custom HttpRequestInitializer for automatic retry upon failures. | |
HttpRequestInitializer httpRequestInitializer = new RetryHttpInitializerWrapper(credential); | |
//POST https://www.googleapis.com/upload/storage/v1/b/myBucket/o?uploadType=media&name=myObject | |
GenericUrl requestUrl = new GenericUrl("https://www.googleapis.com/upload/storage/v1/b/"+bucket+"/o?uploadType=resumable&name="+name); | |
MediaHttpUploader uploader = new MediaHttpUploader(mediaContent, httpTransport, httpRequestInitializer); | |
uploader.setProgressListener(new CustomProgressListener()); | |
HttpResponse response = uploader.upload(requestUrl); | |
if (!response.isSuccessStatusCode()) { | |
throw GoogleJsonResponseException.from(JSON_FACTORY, response); | |
} | |
} | |
/** | |
* Uploads data to an object in a bucket. | |
* | |
* @param name the name of the destination object. | |
* @param contentType the MIME type of the data. | |
* @param stream the data - for instance, you can use a FileInputStream to upload a file. | |
* @param bucketName the name of the bucket to create the object in. | |
*/ | |
public static void uploadStream(String name, | |
InputStream stream, | |
String contentType, | |
String bucketName) | |
throws IOException, GeneralSecurityException { | |
InputStreamContent contentStream = new InputStreamContent(contentType, stream); | |
StorageObject objectMetadata = new StorageObject().setName(name); | |
Storage client = getService(name); | |
// Do the insert | |
Storage.Objects.Insert insertRequest = client.objects().insert( | |
bucketName, objectMetadata, contentStream); | |
insertRequest.getMediaHttpUploader().setDisableGZipContent(true); | |
insertRequest.execute(); | |
} | |
/** | |
* Returns an authenticated Storage object used to make service calls to Cloud Storage. | |
*/ | |
private static Storage getService(String applicationName) | |
throws IOException, GeneralSecurityException { | |
GoogleCredential credential = GoogleCredential.getApplicationDefault(); | |
// Depending on the environment that provides the default credentials (e.g. Compute Engine, | |
// App Engine), the credentials may require us to specify the scopes we need explicitly. | |
// Check for this case, and inject the Cloud Storage scope if required. | |
if (credential.createScopedRequired()) { | |
credential = credential.createScoped(StorageScopes.all()); | |
} | |
HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport(); | |
// custom HttpRequestInitializer for automatic retry upon failures. | |
HttpRequestInitializer initializer = new RetryHttpInitializerWrapper(credential); | |
Storage storageService = new Storage.Builder(httpTransport, JSON_FACTORY, credential) | |
.setApplicationName(applicationName) | |
.build(); | |
if (null == storageService) { | |
// Depending on the environment that provides the default credentials (e.g. Compute Engine, | |
// App Engine), the credentials may require us to specify the scopes we need explicitly. | |
// Check for this case, and inject the Cloud Storage scope if required. | |
if (credential.createScopedRequired()) { | |
credential = credential.createScoped(StorageScopes.all()); | |
} | |
storageService = new Storage | |
.Builder(httpTransport, JSON_FACTORY, credential) | |
.setHttpRequestInitializer(initializer) | |
.setApplicationName(applicationName) | |
.build(); | |
} | |
return storageService; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment