Wednesday, June 3, 2015

CloudBlobContainer.CreateIfNotExists gives "The remote server returned an error: (400) Bad Request." Error

 
Hi Developers,

I was creating a small demo to read and write few images into cloud blob storage, while coding I faced this easy but interesting issue/error while calling CreateIfNotExists() method of blob/queue/table client. Error screen shot shown below. it says: 

 

"An unhandled exception of type 'Microsoft.WindowsAzure.Storage.StorageException' occurred in Microsoft.WindowsAzure.Storage.dll Additional information: The remote server returned an error: (400) Bad Request."

 

 

While looking into it inner exception, I do not see much help….

 

 

 

 

Below is my Code snippet:

 
var account = CloudStorageAccount.DevelopmentStorageAccount;
/******************* Blob **************************************/
var blobClient = account.CreateCloudBlobClient();
var container = blobClient.GetContainerReference(@"Images");

container.CreateIfNotExists();

//************ Download ******************
var blob = container.GetBlockBlobReference(@"CompanyLogoUpload.jpg");
string inputblobname = @"D:\Junk\Logo_Uplaod.jpg";
using (FileStream stream = new FileStream(inputblobname, FileMode.Open))
{

       blob.UploadFromStream(stream);

}
 

Highlighted in RED lines give above error….

 

Resolutions:

 

Error message is misleading as it says "400-Bad Request", so we might go and verify Azure Connectivity, are we connecting to right subscription, login credentials, etc.. .

 

But the problem was, I gave Container name which starts with Capital letter e.g. Images. Azure does not allow capital letter in container name. I changes this to images, and it worked. 

 

When you create a container from UI, error is very descriptive and you do not face this issue, but this comes only when you create container from code behind.

 

Easy but good to know !!!!

 

You can also refer at https://msdn.microsoft.com/en-us/library/dd135715.aspx  for more details.