Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check wether a CloudBlobDirectory exists or not?

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");
like image 993
Parv Sharma Avatar asked Feb 08 '12 07:02

Parv Sharma


1 Answers

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.

like image 120
knightpfhor Avatar answered Sep 29 '22 12:09

knightpfhor