There is no way to check if a "folder" exists in Cloud Storage.
This might sound strange, but you have to consider that folders don’t actually exist in a bucket-based storage. Cloud Storage doesn’t actually have any folders.
The files in the storage just have path metadata associated with them, so that we humans can think hierarchically like we do with folders.
If you want to know if a file exists (not a "folder"), then in your code you could await getMetadata(); on a StorageReference that refers to the file you’re looking for.
A workaround could be to create a dummy file such as "readme.md" inside each folder; that would certify its existence. If you can’t find such file, your folder (probably) doesn’t exist. This implies you carefully add such "dummy" file every time you add a folder.
The answer from @venir is useful in understanding what’s going on but you can overcome the problem by using this approach.
You can check if a folder exists by checking whether its parent folder contains a folder named after the one you are looking for. Something like this (excuse the TypeScript):
Obviously, this only works if there is a parent folder, but often this will be the case. You have to not like Firebase very much for making things so hard!
3
Answers
There is no way to check if a "folder" exists in Cloud Storage.
This might sound strange, but you have to consider that folders don’t actually exist in a bucket-based storage. Cloud Storage doesn’t actually have any folders.
The files in the storage just have path metadata associated with them, so that we humans can think hierarchically like we do with folders.
If you want to know if a file exists (not a "folder"), then in your code you could
await getMetadata();
on aStorageReference
that refers to the file you’re looking for.A workaround could be to create a dummy file such as "readme.md" inside each folder; that would certify its existence. If you can’t find such file, your folder (probably) doesn’t exist. This implies you carefully add such "dummy" file every time you add a folder.
The answer from @venir is useful in understanding what’s going on but you can overcome the problem by using this approach.
You can check if a folder exists by checking whether its parent folder contains a folder named after the one you are looking for. Something like this (excuse the TypeScript):
Obviously, this only works if there is a parent folder, but often this will be the case. You have to not like Firebase very much for making things so hard!