Search code examples
azureazure-storageazure-blob-storageazure-storage-emulator

Can't create azure blob container on storage emulator


I'm unable to create a container while using Azure Storage Emulator from my c# .NET code.

I am using:

var container = serviceClient.GetContainerReference("media");
container.CreateIfNotExists();`

It return the error Error:

System.AggregateException: One or more errors occurred. ---> Microsoft.WindowsAzure.Storage.StorageException: The remote server returned an error: (403) Forbidden. ---> System.Net.WebException: The remote server returned an error: (403) Forbidden. at System.Net.HttpWebRequest.GetResponse()


Solution

  • Add the following line:

    request.UseDefaultCredentials = true;
    

    This will let the application use the credentials of the logged in user to access the site. If it's returning 403, clearly it's expecting authentication.

    It's also possible that you (now?) have an authenticating proxy in between you and the remote site. In which case, try:

    request.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
    

    You could set a connection string to the storage emulator in an app.config:

    <appSettings>
      <add key="StorageConnectionString" value="UseDevelopmentStorage=true" />
    </appSettings>
    

    If you want to connect to storage emulator using account name and key, you would need to provide additional details like different endpoints.

    var connectionString = @"DefaultEndpointsProtocol=http;AccountName=devstoreaccount1;
    AccountKey=Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==;
        BlobEndpoint=http://127.0.0.1:10000/devstoreaccount1;
        TableEndpoint=http://127.0.0.1:10002/devstoreaccount1;
        QueueEndpoint=http://127.0.0.1:10001/devstoreaccount1;";
    

    This value is identical to the shortcut shown above, UseDevelopmentStorage=true.