Search code examples
javagoogle-cloud-platformgoogle-cloud-storage

How to read file from GCP bucket?


below code i'm using to read file

 Map<String, InputStream> allFiles = this.storageService.getObjectsInDirectory(path, bucket);

it is reading file from all directories and its sub directories just want to read files from directory only.


Solution

  • As explained in this document-

    Cloud Storage operates with a flat namespace, which means that folders don't actually exist within Cloud Storage. If you create an object named folder1/file.txt in the bucket your-bucket, the path to the object is your-bucket/folder1/file.txt, but there is no folder named folder1; instead, the string folder1 is part of the object's name.

    Hence, they are emulated using the prefix and delimiter parameters to the List Objects method.

    For example, if you have folder1 with img1 and sub-folder1. If delimiter is set to "/" and prefix is not set, then it will return only img1 and sub-folder1. And If delimiter is set to "/" and prefix is set to "sub-folder1/", it will list objects from the subfolder also.

    You can also check this code sample for listing objects