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.

13 comments:

  1. There seems to be some other reason for this error. i have containername all in small case but still i see the same error. below is code snippet,
    container creation throws same exception as mentioned in article above. Any help would be appreciated.
    //create a container
    CloudBlobContainer cloudBlobContainer = cloudBlobClient.GetContainerReference("imagecontainer");
    //create a container if it is not already exists
    if (cloudBlobContainer.CreateIfNotExists())
    {

    ReplyDelete
  2. did you tried creating the same container from portal, do you see any error on portal? also drop me full code. this should be easy to solve.

    ReplyDelete
  3. Hi Tushar, is it possible to automatically generate GetBlockBlobReference(string) for CloudBlockBlob?? I mean the string value must change for every new blob like we have a method named CreateIfNotExists() if there is no reference Because i have a looping to store a blob.

    ReplyDelete
  4. Unfortunatly No. Because every blob in Azure storage must reside in a container. The container forms part of the blob name. so while accessing you have to follow the same hierarchy account -> Containt -> Blob.

    ReplyDelete
  5. Excellent article. Very interesting to read. I really love to read such a nice article. Thanks! keep rocking.
    Azure Online Course Bangalore

    ReplyDelete