问题
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.
回答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)
回答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.
回答3:
Following gist would do the trick to list all files/folders in a Google Cloud Storage bucket:
https://gist.github.com/hkarakose/d8f9ff78f7deb692e5302e06e8612ca6
You need following maven dependency to run the code:
<dependency>
<groupId>com.google.cloud</groupId>
<artifactId>google-cloud</artifactId>
<version>0.8.0</version>
</dependency>
来源:https://stackoverflow.com/questions/14866206/file-listing-from-google-cloud-storage