Skip to content

Instantly share code, notes, and snippets.

@DazWilkin
Last active September 22, 2017 01:14
Show Gist options
  • Save DazWilkin/cb1839a597bc35ea51a9e6b48041fa5b to your computer and use it in GitHub Desktop.
Save DazWilkin/cb1839a597bc35ea51a9e6b48041fa5b to your computer and use it in GitHub Desktop.
Medium:170921: Cloud Storage w/ API Client Library for Java
package com.google.dazwilkin;
import com.google.api.client.googleapis.auth.oauth2.GoogleCredential;
import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
import com.google.api.client.http.HttpTransport;
import com.google.api.client.json.JsonFactory;
import com.google.api.client.json.jackson2.JacksonFactory;
import com.google.api.client.util.Lists;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.StorageScopes;
import com.google.api.services.storage.model.Bucket;
import com.google.api.services.storage.model.Buckets;
import com.google.api.services.storage.model.Objects;
import com.google.api.services.storage.model.StorageObject;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class CloudStorage
{
private static final String APPLICATION_NAME = "GoogleAPIClient-CloudStorage";
private static final String PROJECT_ID = "[[YOUR-PROJECT-ID]]";
private static final String BUCKET_NAME = "[[BUCKET]]";
private static HttpTransport httpTransport;
private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();
private static final List<String> SCOPES = Arrays.asList(
StorageScopes.DEVSTORAGE_READ_WRITE
);
public static void main( String[] args )
{
try {
httpTransport = GoogleNetHttpTransport.newTrustedTransport();
GoogleCredential credential = GoogleCredential.getApplicationDefault();
Storage storage = new Storage.Builder(
httpTransport,
JSON_FACTORY,
credential
).setApplicationName(
APPLICATION_NAME
).build();
Storage.Buckets.List bucketsList = storage.buckets().list(PROJECT_ID);
Buckets buckets;
do {
buckets = bucketsList.execute();
List<Bucket> items = buckets.getItems();
if (items != null) {
for (Bucket bucket: items) {
System.out.println(bucket.getName());
}
}
bucketsList.setPageToken(buckets.getNextPageToken());
} while (buckets.getNextPageToken() != null);
Storage.Objects.List objectsList = storage.objects().list(BUCKET_NAME);
Objects objects;
do {
objects = objectsList.execute();
List<StorageObject> items = objects.getItems();
if (items != null) {
for (StorageObject object : items) {
System.out.println(object.getName());
}
}
objectsList.setPageToken(objects.getNextPageToken());
} while (objects.getNextPageToken() != null);
} catch (IOException e) {
System.err.println(e.getMessage());
} catch (Throwable t) {
t.printStackTrace();
}
System.exit(1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment