I am trying to get a newly storage account Key with C# but it gives me exception that
Exception Message - The storage account provisioning state must be 'Succeeded' before executing the operation. Stack Trace - at Microsoft.Azure.Management.Storage.StorageAccountsOperations.d__12.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at Microsoft.Azure.Management.Storage.StorageAccountsOperationsExtensions.d__15.MoveNext() --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() at PPS.Controllers.ManageResourcesController.d__33.MoveNext()
In my requirement which is that I need to create a new storage account with a specified location and upload files in it. I am able to create a new storage account through Azure REST API but when I try to get the newly storage account key it gives me exception as I explained above. Below is the code which I am using for creation and get the storage account key
string token = GetAccessToken();
var Credentials = new TokenCredentials(token);
var resourceManagementClient = new ResourceManagementClient(Credentials);
resourceManagementClient.SubscriptionId = SubscriptionID;
var storageProvider = resourceManagementClient.Providers.Register("Microsoft.Storage");
var storageManagementClient = new StorageManagementClient(Credentials);
storageManagementClient.SubscriptionId = SubscriptionID;
string rgName = "Test" + location.Trim().Replace(" ", string.Empty);
var resourceGroup = new Microsoft.Azure.Management.Resources.Models.ResourceGroup
{
Location = location
};
var rgResult = resourceManagementClient.ResourceGroups.CreateOrUpdate(rgName, resourceGroup);
string stAccName = "TCStAccount"+Guid.NewGuid().ToString().Substring(0, 8);
stAccName = stAccName.ToLower();
sku sk = new sku();
sk.name = "Standard_LRS";
storagesettings st = new storagesettings();
st.kind = "Storage";
st.location = location;
st.sku = sk;
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))
{
var payloadText = JsonConvert.SerializeObject(st);
writer.Write(payloadText);
writer.Flush();
stream.Flush();
stream.Position = 0;
using (var content = new StreamContent(stream))
{
content.Headers.Add("Content-Type", "application/json");
content.Headers.Add("x-ms-version", "2016-12-01");
Uri uri = new Uri(String.Format("https://management.azure.com/subscriptions/{0}/resourceGroups/{1}/providers/Microsoft.Storage/storageAccounts/{2}?api-version=2016-12-01", SubscriptionID, rgName, stAccName));
var response = await client.PutAsync(uri, content);
string detailError = await response.Content.ReadAsStringAsync();
if (!response.IsSuccessStatusCode)
{
throw new InvalidOperationException(response.ReasonPhrase);
}
}
}
}
var storageAccountKey = await storageManagementClient.StorageAccounts.ListKeysAsync(rgName, stAccName);
string storage_Account_Key = storageAccountKey.Keys[0].Value;
Let me know If I miss anything.