Search code examples
azure-storageazure-sdk-.netazure-sdk

Creating Azure BlobClient from Uri and connection string


I want to create a Azure SDK BlobClient knowing the blob Uri. I can do it like that :

    StorageSharedKeyCredential storageCredential = new StorageSharedKeyCredential("devstoreaccount1", "account key");
    BlobClient bl = new BlobClient(new Uri(blobUri), storageCredential);

But I do not want to use the StorageSharedKey in this case. I want to use the connection string.

However the constructor taking a connection string as first parameter looks like this :

enter image description here

Is there another way to initialize the BlobClient with Blob Uri + connection string ? If not, since all I have as input is the Blob Url, is there a way to parse the Url in order to isolate the container name and the blob name ? I don't see how to identify them.


Solution

  • Kind of hacky solution but you can try something like this:

            BlobClient blobClient = new BlobClient(new Uri("blob-uri"));
            var containerName = blobClient.BlobContainerName;
            var blobName = blobClient.Name;
            blobClient = new BlobClient(connectionString, containerName, blobName);