Get Google Cloud Storage object list in Java

Introduction

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

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();
    }
}

Run

When using JSON Key

$ ./gradlew run -Pargs="/path/to/key.json"

When not in use

$ ./gradlew run

in conclusion

The code for this time is here: pencil: https://github.com/nownabe/examples/tree/master/list-gcs-java

Recommended Posts

Get Google Cloud Storage object list in Java
[IBM Cloud] Place the ISO file in the object storage
Apply Google Java Style formatter in IntelliJ
Get mail using Gmail API in Java
Get Google Fit API data in Python
[GCP] Operate Google Cloud Storage with Python
Implemented in Dataflow to copy the hierarchy from Google Drive to Google Cloud Storage
Get the EDINET code list in Python
Get Cloud Logging available in Python in 10 minutes
Operate Sakura's cloud object storage from Python
Get only the subclass elements in a list
Get a panoramic image in Google Street View
Get Google Image Search images in original size
What to do to get google spreadsheet in python
[Cloud Functions] Automatically decompress GZIP files placed in Storage
[python] Get the list of classes defined in the module
URL encoding process in GAE / py's Cloud Storage library
Python: Get a list of methods for an object
A story linked with Google Cloud Storage with a little ingenuity
Use of Google Cloud Storage (GCS) with "GAE / Py"