Given a blob storage connection string such as:
DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar;EndpointSuffix=core.windows.net
Is there a known Microsoft object that this can be converted / deserialized into? I don't want to actually parse the string, but I need to extract the AccountName and AccountKey from the entire connection string, which I have as a string.
To pre-empt possible "Why do you want to do this?" questions... I have an existing class that requires the connection string to be injected as a string. To avoid breaking changes, I can't alter that. But I do need to add some methods in this class that need the AccountName and AccountKey as individual items.
Thanks!
You can find your storage account's connection strings in the Azure portal. Navigate to SETTINGS > Access keys in your storage account's menu blade to see connection strings for both primary and secondary access keys.
To get a connection string with PowerShell, first get a StorageAccountContext object, then retrieve the ConnectionString property. To get a connection string with Azure CLI, call the az storage account show-connection-string command.
The Query Blob Contents API applies a simple Structured Query Language (SQL) statement on a blob's contents and returns only the queried subset of the data. You can also call Query Blob Contents to query the contents of a version or snapshot.
If you install Microsoft.Azure.Storage.Common
, you can extract several bits of your connection string programmatically, without parsing the connection string yourself.
For example (with actual info obfuscated):
using System;
using Microsoft.Azure.Storage;
namespace dotnet_connectionstring
{
class Program
{
static void Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=youraccount;AccountKey=yourkey==;EndpointSuffix=core.windows.net");
Console.WriteLine(storageAccount.BlobEndpoint);
Console.WriteLine("---");
Console.WriteLine(storageAccount.BlobStorageUri);
Console.WriteLine("---");
Console.WriteLine(storageAccount.Credentials.AccountName);
Console.WriteLine("---");
Console.WriteLine(storageAccount.Credentials.ExportBase64EncodedKey());
}
}
}
This gives output something like:
https://youraccount.blob.core.windows.net/ --- Primary = 'https://youraccount.blob.core.windows.net/'; Secondary = 'https://youraccount-secondary.blob.core.windows.net/' --- youraccount --- yourkey==
There are no classes that I know of that do this, but it wouldn't be that hard to change it into a dictionary. Example below.
string connString = "DefaultEndpointsProtocol=https;AccountName=foo;AccountKey=bar;EndpointSuffix=core.windows.net";
var connStringArray = connString.Split(';');
var dictionary = new Dictionary<string, string>();
foreach (var item in connStringArray)
{
var itemKeyValue = item.Split('=');
dictionary.Add(itemKeyValue[0], itemKeyValue[1]);
}
Then you could access the values you need using this.
dictionary["AccountName"]
dictionary["AccountKey"]
The answer of @David Makogon is certainly the most elegant but the package Microsoft.Azure.Storage.Common
is deprecated (as stated in comments). Based on @Patrick Mcvay answer (which is just a bit bugged as there might be '=' in the connection string values), an easy way to parse a connection string would be:
var parsedConnectionString = new Dictionary<string, string>();
foreach (var item in ConnectionString.Split(';'))
{
var idx = item.IndexOf('=');
parsedConnectionString[item.Substring(0, idx)] =
item.Substring(idx + 1, item.Length - idx - 1);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With