Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

We are able to get container name using the code below:

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 will not return data as expected. In this case, we have to use the CloudBlockBlob object.

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

Is there any method to get a container name that will work for URLs listed below:

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
like image 793
Kaarthikeyan Avatar asked Jan 01 '23 06:01

Kaarthikeyan


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

like image 98
Gaurav Mantri Avatar answered Jan 02 '23 21:01

Gaurav Mantri