Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get list of files in Cloud Storage (Java)

Is there any possibility to list all files on my Google Cloud Storage bucket with the GAE SDK? I know that the Python SDK supports such a function, but I can't find a similar function in the Java SDK.

If not available, will this be added in the future releases of the Java SDK?

like image 342
Eich Avatar asked Nov 16 '12 13:11

Eich


People also ask

What is blob in Google Cloud Storage?

An object in Google Cloud Storage. A Blob object includes the BlobId instance, the set of properties inherited from the BlobInfo class and the Storage instance. The class provides methods to perform operations on the object. Reading a property value does not issue any RPC calls.


2 Answers

You can also do this using the Google Java Client Library (which is replacing the Google Cloud Storage API)

GcsService gcsService = GcsServiceFactory.createGcsService(RetryParams.getDefaultInstance());
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();

ListResult result = gcsService.list(appIdentity.getDefaultGcsBucketName(), ListOptions.DEFAULT);
while (result.hasNext()){
    ListItem l = result.next();
    String name = l.getName();

    System.out.println("Name: " + name);
}

If you only want to iterate through a certain "directory", use the ListOptions builder

ListOptions.Builder b = new ListOptions.Builder();
b.setRecursive(true);
b.setPrefix("directory");
...

ListResult result = gcsService.list(appIdentity.getDefaultGcsBucketName(), b.build());
...
like image 125
Ian Avatar answered Oct 06 '22 04:10

Ian


You can use the Cloud Storage JSON API via its client library. Once you set up your credentials you can make the call like this:

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

You may want to use an AppIdentityCredential in this case as well, which will allow the bucket to be owned by your application, and not by a user.

like image 30
Jason Hall Avatar answered Oct 06 '22 05:10

Jason Hall