I am having an issue trying to determine if a 'directory/folder' exists in Google Cloud Storage.
I know there is technically no concept of a 'directory' or 'folder' but I need to check if a particular prefix exists.
Here is the way i'm detecting if a Blob exists which works fine:
public boolean doesFileExist(String bucket, String prefix) {
Blob blob = storage.get(bucket, prefix);
return blob != null;
}
This seems to work when using an actual filename with an extension. However, using this for something like folder/
does not work.
Any suggestions?
You can use the Cloud Storage List API to query for all files with a shared prefix. If you find any files at all, then that means it exists. You will want to use the list() method and pass a set of BlobListOption that specify the prefix, and perhaps just a page size of 1 for efficiency.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With