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" />
I actually ended up finding the problem.
My problem was that the blob storage emulator would not start (the other emulators would start and I missed the blob). The problem ended up being that the port 10000 (default blob emulator port) was already being used by another software. I used
Netstat
cmd tool to see which software it was, killed it and its now working like a charm!!! Thanks everyone!!If you have just updated the WindowsAzure.Storage nuget package and your app started crashing with http error 400 bad request :
In my case it happened when I updated to 8.2.1 and my local emulator was version 5.1.
My solution is this :
When I downloaded Storage Emulator 5.2 and upgraded from 5.1 the errors stopped. Such an error happened several times to me already.
My humble request if anybody from Microsoft Azure Storage Emulator team reads this - Please add a check for development mode and throw a meaningful exception with the message like - "You have Azure Storage Emulator version X.Y.Z installed. In order to use the current WindowsAzure.Storage library **V.V.V with Azure Emulator you need to install version Z.Z.Z of the emulator from this link".** or whatever you consider useful.
This kind of problem have wasted several hours of my time and I suppose the same happened to thousands of developers around the world and still this exception sits there - for more than 4 years!
it's necessary view the httpstatusmessage in exception: in my case the error was because The requested URI does not represent any resource on the server.
so i've seen that my
BlobContainerName
not contains the right container (or not exists)other case i've seen is the wrong name of container. blobcontainername that must be a name like "mycontainer1" or "mycontainer2" and so on
here the code to add container
As you found through your research, the problem is the name.
You say that your test container is named
imageads-57905553-8585-4d7c-8270-be9e611eda81
, but in your code you are usingImageAds-57905553-8585-4d7c-8270-be9e611eda81
. Notice the difference in capitalization. If you switch your container name to all lower case it will work correctly.For more information, see #3 under Container Names at Naming and Referencing Containers, Blobs, and Metadata:
Make sure your versions of the storage libraries and storage emulator aren't "out of sync". I updated my libraries but didn't update the emulator to the latest version and got this exact situation.