简体   繁体   English

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

[英]Get Storage Account Key of newly create storage account

I am trying to get a newly storage account Key with C# but it gives me exception that 我正在尝试使用C#获取一个新的存储帐户密钥,但是它给了我一个例外

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() 堆栈跟踪-在Microsoft.Azure.Management.Storage.StorageAccountsOperations.d__12.MoveNext()-从上一个引发异常的位置开始的堆栈跟踪结束-在System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() Microsoft.Azure.Management.Storage.StorageAccountsOperationsExtensions.d__15.MoveNext()上的.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)---从上一个引发异常的位置开始的堆栈结束跟踪---在System.Runtime。 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)处的ExceptionServices.ExceptionDispatchInfo.Throw()System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()处的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. 我可以通过Azure REST API创建一个新的存储帐户,但是当我尝试获取新的存储帐户密钥时,它给了我上述的异常。 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. 让我知道是否想念任何事情。

According to the error message, you could find: 根据错误消息,您可以找到:

The storage account provisioning state must be 'Succeeded' before executing the operation. 在执行操作之前,存储帐户设置状态必须为“成功”。

After you have created the storage account, azure will begin creating the storage account. 创建存储帐户后,azure将开始创建存储帐户。

It will take some time, almost 10 seconds. 这将花费一些时间,将近10秒钟。

Before the storage account created successfully, you couldn't get the storage access key. 在成功创建存储帐户之前,您无法获取存储访问密钥。

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()方法等待成功创建的存储帐户,然后再次发送请求以询问存储帐户密钥。

It will work well. 它将运作良好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM