Unable to create Storage account on Azure C#

981 views Asked by At

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

Exception

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

2

There are 2 answers

5
Bruce Chen On

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:

enter image description here

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:

enter image description here

0
Mike On

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.