【问题标题】:Get Storage Account Key of newly create storage account获取新创建的存储帐户的存储帐户密钥
【发布时间】:2018-02-27 09:30:14
【问题描述】:

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

异常消息 - 在执行操作之前,存储帐户配置状态必须为“成功”。 堆栈跟踪 - 在 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; 

如果我错过了什么,请告诉我。

【问题讨论】:

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


    【解决方案1】:

    根据报错信息可以找到:

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

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

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

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

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

    它会很好用。

    【讨论】:

    • You could use thread.sleep() method to wait the storage account created successfully, then send the request again to ask the storage account key. - 恕我直言,更好的选择是获取存储帐户属性并定期检查配置状态(使用 thread.sleep()),并且仅在配置状态成功时获取存储帐户密钥.
    猜你喜欢
    • 2017-05-01
    • 2017-08-28
    • 1970-01-01
    • 2019-09-17
    • 2019-01-14
    • 2020-11-05
    • 2018-10-09
    • 2020-01-20
    • 2017-01-06
    相关资源
    最近更新 更多