File listing from google cloud storage

11,840

Solution 1

This should help you.

Please remember that you need the Cloud Storage API and not the GAE API (just FYI :) . You can read about that API here -> https://developers.google.com/storage/docs/json_api/v1/json-api-java-samples (JSON, there is also a XML API)

Solution 2

The easiest way to access Google Cloud Storage buckets from Java is to use the NIO Filesystem Provider for Google Cloud Storage.

The link shows how to add the dependency to your project and set things up. Then, to list the files in a bucket:

try (FileSystem fs = CloudStorageFileSystem.forBucket("bucket-name")) {
  for (Path path : Files.newDirectoryStream(fs.getPath("folder-name/"))) {
        System.out.println(path);
      }
}

I haven't tried this in AppEngine itself, but I tested it outside and it works.

Share:
11,840
alperkin
Author by

alperkin

Updated on June 09, 2022

Comments

  • alperkin
    alperkin almost 2 years

    For a project I'm doing, I will have files stored in Google's Cloud Storage and am building a web app to interface to those files. I would like my app to show a list of the files (or objects may be the appropriate name) stored in my bucket. I'm completely new to web development and google apis.

    I've been researching how to do this and have found this bit of code...

    Storage storage = new Storage(httpTransport, jsonFactory, credential);
        ObjectsList list = storage.objects().list("bucket-name").execute();
        for (Object obj : list.getItems()) {
    
          }
    

    and it is stated to use an AppIdentityCredential.

    Any advice on how to use the above code along with an AppIdentityCredential or any advice on listing files stored in a bucket using java would be greatly appreciated.

  • Jean-Philippe Murray
    Jean-Philippe Murray over 9 years
    Links are dead. Any idea of new ones?