Search code examples
azureazure-storageazure-fluent-api

Unable to get storage account key with Azure Fluent


I created a storage account with Azure Fluent SDK. But after I created the storage account, I wanted to get the name and key to build the connection string that I can use to access the storage account. The problem is that the 'Key' property is a Guid, not the key shown in the Azure Portal.

This is how I create the storage account.

IStorageAccount storage = azure.StorageAccounts.Define(storageAccountName)
    .WithRegion(Region.USEast)
    .WithNewResourceGroup(rgName)
    .Create();

How can I get the proper Key to build the connection string?


Solution

  • You should be able to do it via the below code, this documentation also shows the use of Fluent but only the auth methods:

        // Get a storage account
    var storage = azure.StorageAccounts.GetByResourceGroup("myResourceGroup", "myStorageAccount");
    
    // Extract the keys
    var storageKeys = storage.GetKeys();
    
    // Build the connection string
    string storageConnectionString = "DefaultEndpointsProtocol=https;"
            + "AccountName=" + storage.Name
            + ";AccountKey=" + storageKeys[0].Value
            + ";EndpointSuffix=core.windows.net";
    
    // Connect
    var account = CloudStorageAccount.Parse(storageConnectionString);
    
    // Do things with the account here...