I'm developing an ASP.Net MVC 4 app and I'm using Azure Blob to store the images that my users are going to upload. I have the following code:
var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString);
var blobStorage = storageAccount.CreateCloudBlobClient();
//merchantKey is just a GUID that is asociated with the merchant
var containerName = ("ImageAds-" + merchant.merchantKey.ToString()).ToLower();
CloudBlobContainer container = blobStorage.GetContainerReference(containerName);
if (container.CreateIfNotExist())
{
//Upload the file
}
as soon as the if statement is excecuted I'm getting the following exception:
{"The remote server returned an error: (400) Bad Request."}
I thought it was the container's name but I don't see anything wrong with it. The connection string seems to create a good storage with all details for the blob. I'm at a loss. I've researched the web and everyone is saying it's a naming problem but I can't find anything wrong with it.
Test Container name that I used: imageads-57905553-8585-4d7c-8270-be9e611eda81
The Container has the following uri: {http://127.0.0.1:10000/devstoreaccount1/imageads-57905553-8585-4d7c-8270-be9e611eda81}
UPDATE:
I have changed the container name to just image
and I still get the same exception. also the development connection string is as follows: <add name="StorageConnection" connectionString="UseDevelopmentStorage=true" />
From experimentation it appears as though container names must always also be lower case. There must be an implicit conversion internally, which causes it to create the original blob in lower case, but not when it compares it in createifnotexists(async). But when it goes to re-create it, it lower cases it again, which results in a conflict. This is a best guess.
To expand on @kwill's answer, I implemented a solution for converting any string into an acceptable container name, based on Azure's rules for container naming:
Then, when you try to get the container, clean it up first:
I tried reproducing your issue, but it looks like you are using an older version of the client library, since
container.CreateIfNotExist()
is nowcontainer.CreateIfNotExists()
. Have you considered upgrading the the latest client version (2.1)?I just had this issue and fixed it.
My container name was fine, but I accidentally had the AccountName parameter in my connection string capitalized. This led to my 400.
Mine was a stupid naming problem! Apparently we are not allowed to use uppercase in the names.
I've just changed this:
To
I encountered this error after updating the packages but not my code. My issue is that the connection string format and content has changed since I first started using Azure Storage several years ago. Make sure to update your connection string appropriately from the access keys options within the azure portal.
In my case: I was missing this in my connection string: EndpointSuffix=core.windows.net