How can i download folder from gcp bucket using java?

Ankit :

Currently, I'm using this code

Storage storage = StorageOptions.newBuilder()
                                .setCredentials(credentials)
                                .setProjectId(projectId)
                                .build()
                                .getService();
Blob blob = storage.get(BlobId.of(bucketName, objectName));
blob.downloadTo(FilePath);

to download a specific file from my bucket but if I have to download a folder containing multiple files, I'm unable to do it.

Chris32 :

In Google Cloud Platform Storage there is not such thing as folder. Instead there is a virtual representation of them but in the reality you just have a bunch of files with "similar" names grouped in a hierarchical file tree.

i.e.

If you have gs://bucket/folder1/folder2/objectA and gs://bucket/folder1/folder2/objectB

Both files are virtually under the same folder but in the reality you just have 2 objects in your bucket with similar names

For the object A the filename is "bucket/folder1/folder2/objectA" and for the file b the filename is "bucket/folder1/folder2/objectB"

A folder is intepreted when you have a route ending in a "/" character such as "bucket/folder1/folder2/"

In this case, you'll need to use the object listing method to iterate over the objects you're interested in and individually make a call to download each one.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=20823&siteId=1