How to get the Google Cloud Storage object list in Java.
I had a hard time finding a simple way to authenticate or get only a specific directory: tired_face:
build.gradle
Just google-cloud-storage
is OK.
build.gradle
apply plugin: 'java'
apply plugin: 'application'
repositories {
mavenCentral()
}
dependencies {
compile 'com.google.cloud:google-cloud-storage:0.9.4-beta'
}
mainClassName = "GCSList"
run {
if (project.hasProperty('args')) {
args project.args.split('\\s+')
}
}
Code that outputs a list to objects under a specific directory.
You can now specify the JSON Key path for the service account as a command line argument. If there is no argument, the default account will be used. [^ 1]
[^ 1]: Something like gcloud auth login
src/main/java/GCSList.java
import com.google.auth.oauth2.ServiceAccountCredentials;
import com.google.cloud.Page;
import com.google.cloud.storage.Blob;
import com.google.cloud.storage.Bucket;
import com.google.cloud.storage.Storage;
import com.google.cloud.storage.StorageOptions;
import java.io.*;
import java.util.Iterator;
public class GCSList {
private static final String BUCKET = "mybucket";
private static final String PREFIX = "dir1/dir2/";
public static void main(String args[]) throws IOException {
Storage storage;
if (args.length > 0) {
storage = getStorageFromJsonKey(args[0]);
} else {
storage = StorageOptions.getDefaultInstance().getService();
}
Bucket bucket = storage.get(BUCKET);
//Narrow down to specific directories
Storage.BlobListOption option = Storage.BlobListOption.prefix(PREFIX);
Page<Blob> blobs = bucket.list(option);
Iterator<Blob> blobIterator = blobs.iterateAll();
while (blobIterator.hasNext()) {
System.out.println(blobIterator.next().getName());
}
}
private static Storage getStorageFromJsonKey(String key) throws IOException {
return StorageOptions.newBuilder()
.setCredentials(ServiceAccountCredentials.fromStream(new FileInputStream(key)))
.build()
.getService();
}
}
When using JSON Key
$ ./gradlew run -Pargs="/path/to/key.json"
When not in use
$ ./gradlew run
The code for this time is here: pencil: https://github.com/nownabe/examples/tree/master/list-gcs-java
Recommended Posts