Unable to create Storage account on Azure C#

喜欢而已 提交于 2019-12-10 23:24:56

问题


I am trying to create a storage from c# in asp.net MVC application but it throws me error Account type StandardLRS is invalid as you can see below

Below is the code which I am using for creation

string token = GetAccessToken("sub id", "app name", "tenant id");
                var Credentials = new TokenCredentials(token);

                var resourceManagementClient = new ResourceManagementClient(Credentials);
                resourceManagementClient.SubscriptionId = "sub id";

                var storageProvider = resourceManagementClient.Providers.Register("Microsoft.Storage");

                var storageManagementClient = new StorageManagementClient(Credentials);
                storageManagementClient.SubscriptionId = "sub id";

                string rgName = "TimedCloudRG" + location.Trim().Replace(" ", string.Empty);

                var resourceGroup = new Microsoft.Azure.Management.Resources.Models.ResourceGroup
                {
                    Location = location
                };
                var rgResult = resourceManagementClient.ResourceGroups.CreateOrUpdate(rgName, resourceGroup);

                Microsoft.Azure.Management.Storage.Models.Sku DefaultSku = new Microsoft.Azure.Management.Storage.Models.Sku(Microsoft.Azure.Management.Storage.Models.SkuName.StandardLRS);
                Microsoft.Azure.Management.Storage.Models.Kind DefaultKind = Microsoft.Azure.Management.Storage.Models.Kind.Storage;                       
                Microsoft.Azure.Management.Storage.Models.StorageAccountCreateParameters parameters = new Microsoft.Azure.Management.Storage.Models.StorageAccountCreateParameters()
                {
                    Kind = DefaultKind,
                    Sku = DefaultSku,                    
                    Location = rgResult.Location
                };


                string stAccName = "TCStorageAccount"+Guid.NewGuid().ToString().Substring(0, 8);

                var storageAccount = await storageManagementClient.StorageAccounts.CreateAsync(rgName, stAccName, parameters);

Surprisingly it works fine when I run it on console application. I also try to install Microsoft.Azure.Management.Storage.Fluent package but unable to install as my project .net framework selected as 4.5.1 and this package not supported to that version.

Any help would be greatly appreciated. Thanks


回答1:


According to your description, I leveraged Microsoft.Azure.Management.Storage 6.2.0-preview which supports .NETFramework 4.5 to check this issue. I specified the Location as West US and I could create my storage account.

string stAccName = "TCStorageAccount"+Guid.NewGuid().ToString().Substring(0, 8);

AFAIK, your account name is not a valid storage account name. Storage account name must be between 3 and 24 characters in length and use numbers and lower-case letters only.

I tried to create storage account on Azure Portal and accidentally found the following result:

As Locally redundant storage mentioned as follows:

Some applications are restricted to replicating data only within a country due to data governance requirements. A paired region could be in another country. For more information on region pairs, see Azure regions.

Additionally, you could use Microsoft.Azure.Management.Storage.Fluent 1.0.0 which supports .NETFramework 4.5.

UPDATE:

I created a sample AspDotNet-MVC-CreateAzureStorageAccount and defined the CreateAzureStorageAccount action for creating storage account under HomeController.cs. Here is the test result, you could refer to it as follows:




回答2:


I ran into a similar problem calling the REST API's directly. I was missing the underscore between Standard and LRS. The AccountType needs to be specified exactly as seen in the ARM templates, in this case Standard_LRS or Standard_GRS.



来源:https://stackoverflow.com/questions/45984311/unable-to-create-storage-account-on-azure-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!