In the software that I am programming, I am attempting to create a virtual file system over the blobs structure of Azure.
Many times in the process, I get a path from the system and I need to tell whether the path is of a Blob
or just a virtual BlobDirectory
that azure provides. I did this by casting it from one form to another and handling the error.
But now, if I know that a path points to a virtual directory, how can I check whether this virtual directory exists or not?
I can get the reference to the CloudBlobDirectory
with the following code:
var blobDirectory = client.GetBlobDirectoryReference("Path_to_dir");
In blob storage, directories don't exist as an item by themselves. What you can have is a blob that has a name that can be interpreted as being in a directory. If you look at the underlying REST API you'll see that that there's nothing in there about directories. What the storage client library is doing for you is searching for blobs that start with the directory name then the delimiter e.g. "DirectoryA/DirectoryB/FileName.txt". What this means is that for a directory to exist it must contain a blob. To check if the directory exists you can try either:
var blobDirectory = client.GetBlobDirectoryReference("Path_to_dir");
bool directoryExists = blobDirectory.ListBlobs().Count() > 0
or
bool directoryExists = client.ListBlobsWithPrefix("DirectoryA/DirectoryB/").Count() > 0
I'm aware that listing everything in the directory just to get the count isn't that great an idea, I'm sure you can come up with a better method.
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