获取新创建存储帐户的存储帐户密钥

时间:2017-09-18 18:43:40

标签: c# asp.net-mvc azure arm

我正在尝试使用C#获取一个新的存储帐户密钥,但它给了我一个例外

  

异常消息 - 在执行操作之前,存储帐户配置状态必须为“Succeeded”。   堆栈跟踪 - 在Microsoft.Azure.Management.Storage.StorageAccountsOperations.d__12.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在Microsoft.Azure.Management.Storage.StorageAccountsOperationsExtensions.d__15.MoveNext()   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()      在PPS.Controllers.ManageResourcesController.d__33.MoveNext()

在我的要求中,我需要创建一个具有指定位置的新存储帐户并在其中上传文件。我可以通过Azure REST API创建一个新的存储帐户,但是当我尝试获取新的存储帐户密钥时,它给了我例外,如上所述。以下是我用于创建并获取存储帐户密钥的代码

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; 

让我知道如果我错过了什么。

1 个答案:

答案 0 :(得分:1)

根据错误消息,您可以找到:

  

在执行操作之前,存储帐户配置状态必须为“成功”。

创建存储帐户后,azure将开始创建存储帐户。

这需要一些时间,差不多10秒钟。

在成功创建存储帐户之前,您无法获取存储访问密钥。

您可以使用thread.sleep()方法等待成功创建的存储帐户,然后再次发送请求以询问存储帐户密钥。

它会很好用。