简体   繁体   中英

Azure Blob Storage Invalid Account String Error

I'm having problems connecting to my Azure Blob Storage account in ASP.NET MVC5.

I looked at a few tutorials and came up with a simple repository pattern with a BlobStorageContainerFactory so I could inject named repositories. This code works fine when I use the Azure Storage local development emulator. Here is the code for the factory:

    public class BlobStorageContainerFactory
    {
        private CloudStorageAccount _storageAccount;
        private CloudBlobClient _blobClient;
        public CloudBlobContainer Get(string containerName)
        {
            //i want to get the connection string from the web.config eventually
            //_storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["BattleBornPrepsStorageConnection"].ConnectionString);

            //error occurs here
            _storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=http;AccountName=myaccountnamealllower;AccountKey=theprimaryaccountkeyfromazuremanagementportal;");

            _blobClient = _storageAccount.CreateCloudBlobClient();
            var container = _blobClient.GetContainerReference(containerName);

            if (container.CreateIfNotExist())
            {
                // configure container for public access
                var permissions = container.GetPermissions();
                permissions.PublicAccess = BlobContainerPublicAccessType.Container;
                container.SetPermissions(permissions);
            }

            return container;
        }
    }

I get an error when I try to parse the connection string:

An exception of type 'System.FormatException' occurred in Microsoft.WindowsAzure.StorageClient.dll but was not handled in user code

Additional information: Invalid account string.

Stack Trace:

[FormatException: Invalid account string.]
   Microsoft.WindowsAzure.CloudStorageAccount.<Parse>b__0(String err) +34
   Microsoft.WindowsAzure.CloudStorageAccount.ParseStringIntoSettings(String s, Action`1 error) +344
   Microsoft.WindowsAzure.CloudStorageAccount.TryParse(String s, CloudStorageAccount& accountInformation, Action`1 error) +19
   Microsoft.WindowsAzure.CloudStorageAccount.Parse(String value) +135
   MyProject.Data.BlobStorageContainerFactory.Get(String containerName) in f:\!ClientFiles\MyFiles\MyProject\LocalRepository\trunk\Release\MyProject\MyProject.Data\BlobStorageContainerFactory.cs:19
   MyProject.Data.StorageRepository..ctor(String containerName) in f:\!ClientFiles\MyFiles\MyProject\LocalRepository\trunk\Release\MyProject\MyProject.Data\StorageRepository.cs:20
   MyProject.Data.PublicationStorageRepository..ctor() in f:\!ClientFiles\MyFiles\MyProject\LocalRepository\trunk\Release\MyProject\MyProject.Data\PublicationStorageRepository.cs:12
   DynamicInjectorc89300dbc50f4bd49b75f3ea31ffe3e6(Object[] ) +71
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +884
   Ninject.Activation.Context.ResolveInternal(Object scope) +218
   Ninject.Activation.Context.Resolve() +216
   Ninject.<>c__DisplayClass15.<Resolve>b__f(IBinding binding) +86
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
   System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4098209
   Ninject.Planning.Targets.Target`1.GetValue(Type service, IContext parent) +169
   Ninject.Planning.Targets.Target`1.ResolveWithin(IContext parent) +747
   Ninject.Activation.Providers.StandardProvider.GetValue(IContext context, ITarget target) +269
   Ninject.Activation.Providers.<>c__DisplayClass4.<Create>b__2(ITarget target) +69
   System.Linq.WhereSelectArrayIterator`2.MoveNext() +66
   System.Linq.Buffer`1..ctor(IEnumerable`1 source) +335
   System.Linq.Enumerable.ToArray(IEnumerable`1 source) +77
   Ninject.Activation.Providers.StandardProvider.Create(IContext context) +847
   Ninject.Activation.Context.ResolveInternal(Object scope) +218
   Ninject.Activation.Context.Resolve() +277
   Ninject.<>c__DisplayClass15.<Resolve>b__f(IBinding binding) +86
   System.Linq.WhereSelectEnumerableIterator`2.MoveNext() +145
   System.Linq.Enumerable.SingleOrDefault(IEnumerable`1 source) +4098209
   Ninject.Web.Mvc.NinjectDependencyResolver.GetService(Type serviceType) +145
   Castle.Proxies.Invocations.IDependencyResolver_GetService.InvokeMethodOnTarget() +138
   Castle.DynamicProxy.AbstractInvocation.Proceed() +117
   Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed() +48
   Glimpse.Core.Extensibility.ExecutionTimer.Time(Action action) +195
   Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult) +198
   Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context) +45
   Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation) +183
   Castle.DynamicProxy.AbstractInvocation.Proceed() +483
   Castle.Proxies.IDependencyResolverProxy.GetService(Type serviceType) +187
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +87

[InvalidOperationException: An error occurred when trying to create a controller of type 'MyProject.Ui.Mvc.Areas.Admin.Controllers.PublicationController'. Make sure that the controller has a parameterless public constructor.]
   System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +247
   System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +438
   System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +257
   Castle.Proxies.Invocations.IControllerFactory_CreateController.InvokeMethodOnTarget() +197
   Castle.DynamicProxy.AbstractInvocation.Proceed() +117
   Glimpse.Core.Extensibility.CastleInvocationToAlternateMethodContextAdapter.Proceed() +48
   Glimpse.Core.Extensibility.ExecutionTimer.Time(Action action) +195
   Glimpse.Core.Extensions.AlternateMethodContextExtensions.TryProceedWithTimer(IAlternateMethodContext context, TimerResult& timerResult) +198
   Glimpse.Core.Extensibility.AlternateMethod.NewImplementation(IAlternateMethodContext context) +45
   Glimpse.Core.Extensibility.AlternateTypeToCastleInterceptorAdapter.Intercept(IInvocation invocation) +183
   Castle.DynamicProxy.AbstractInvocation.Proceed() +483
   Castle.Proxies.IControllerFactoryProxy.CreateController(RequestContext requestContext, String controllerName) +202
   System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +326
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +157
   System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +88
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +50
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +301
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

It looks blob storage connection string doesn't allow ; at the end of the string.

And the order of them doesn't matter.

<connectionStrings>      
    <add name="AzureStorageAccount" connectionString="DefaultEndpointsProtocol=https;AccountName=testdata;Accoun‌​tKey=1gy3lpE7Du1j5ljKiupgKzywSw2isjsdfdsfsdfsdsgfsgfdgfdgfd/YThisv/OVVLfIOv9kQ==;"/>
</connectionStrings>

But sometimes it won't work and will through the error

An unhandled exception of type 'System.FormatException' occurred in Microsoft.WindowsAzure.Storage.dll

Additional information: No valid combination of account information found. then please try with below code: tested and working 100%

var accountName = "test2rdsfdg462";
var keyValue = "1gy3lpE7Du1j5ljKiupgKzywSfsdfdsfsdfsdfsdfsdfsdqGxd7/YThisv/OVVLfIOv9kQ==";
var useHttps = true;
var connValid = true;

var storageCredentials = new StorageCredentials(accountName, keyValue);
var storageAccount = new CloudStorageAccount(storageCredentials, useHttps);
var conString = storageAccount.ToString(connValid);

CloudStorageAccount sa = CloudStorageAccount.Parse(connString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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