How to check wether a CloudBlobDirectory exists or not?
Asked Answered
E

4

9

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");
Ephemera answered 8/2, 2012 at 7:33 Comment(0)
C
16

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.

Computation answered 9/2, 2012 at 0:42 Comment(2)
blobDirectory.ListBlobs().Any () would save counting all the blobsPalmore
It's been a while since I've looked at the inner workings of this library, but whether using .Any() would make a different or not will depend a lot on how it deals with large lists of blobs. If a call to ListBlobs() always returns the full set of blobs, then it will only make a very small difference. If ListBlobs() is smart enough to only get fetch blob details on demand and you have more than 5000 blobs in that directory (the underlying REST API only returns them in groups of up to 5000 at a time), then it might save some calls.Computation
P
1
val storageAccountString: String = s"BlobEndpoint=https://$account.$endpoint;SharedAccessSignature=$SASTOKEN"

val client: CloudBlobContainer = CloudStorageAccountparse(storageAccountString).createCloudBlobClient().getContainerReference(container)
    
val blobPattern: String = s"wasbs://$containerName@$accountName.blob.core.windows.net/$dirPath"
    
client
      .getBlockBlobReference(blobPattern)
      .exists
      .booleanValue
Pagurian answered 19/8, 2021 at 10:55 Comment(0)
E
0

Not sure if you can use GetAttributes method and if it raised exception then means no directory exist. I used the similar approach to verify if an blob exist, but didn't tested on a directory yet.

Entomo answered 8/2, 2012 at 12:15 Comment(2)
there is nothing aa FetchAttributes() on a cloudBlobDirectory objectEphemera
I think misunderstood your question. The directory is not physically exists on the blob. Like Knightpfhor said, if you named a blob as "dirA/dirB/a.txt" it will looks like in dirA and dirB directory. I'm following Knightpfhor to check the directory exists.Entomo
G
0

For Java this can be used:

container.getDirectoryReference(directoryName).listBlobs().iterator().hasNext() == true

means directory exists else no directory exists.

Galimatias answered 6/5, 2019 at 11:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.