How to get container name from file URL in Azure Blob Storage C#

Viewed 3572

We are able to get container name using the below code

new CloudBlobContainer(url).Name

But this method works only if the URL is container base URL.

If we are trying to pass the URL of a file and try to get the container name, the code is not returning data as expected. In this case, we have to use the CloudBlockBlob object

new CloudBlockBlob(new Uri(Url)).Container.Name

Whether there is any method to get a container name that will be working for both the below-mentioned URLs http://127.0.0.1:10000/devstoreaccount1/10d59357-b4d1-41e8-ba2a-d92964e1ac53 http://127.0.0.1:10000/devstoreaccount1/10d59357-b4d1-41e8-ba2a-d92964e1ac53/temp/1.txt

1 Answers

Older SDK (9.3.3)

If you're using older SDK (9.3.3), You can use BlobClient and the name of the blob container will be available in BlobContainerName property.

BlobClient client = new BlobClient(new Uri(url));

enter image description here


enter image description here

Newer SDK (12.2.0)

For newer SDK (12.2.0), you can use BlobUriBuilder and the name of the blob container will be available in BlobContainerName property.

BlobUriBuilder blobUriBuilder = new BlobUriBuilder(new Uri(url));

enter image description here


enter image description here

Related