Blog coding and discussion of coding about JavaScript, PHP, CGI, general web building etc.

Tuesday, May 17, 2016

Azure Blob 400 Bad request on Creation of container

Azure Blob 400 Bad request on Creation of container


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 its a naming problem but I can't seem 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:

Answer by kwill for Azure Blob 400 Bad request on Creation of 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 using ImageAds-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:

3. All letters in a container name must be lowercase.

Answer by Garrett for Azure Blob 400 Bad request on Creation of container


I tried reproducing your issue, but it looks like you are using an older version of the client library, since container.CreateIfNotExist() is now container.CreateIfNotExists(). Have you considered upgrading the the latest client version (2.1)?

Answer by hjavaher for Azure Blob 400 Bad request on Creation of container


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!!

Answer by user3394095 for Azure Blob 400 Bad request on Creation of container


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)

CloudBlobContainer container = > blobClient.GetContainerReference(BlobContainerName);  

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

try          {              CloudStorageAccount storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnectionString"].ConnectionString);                // Create the blob client.              CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();                // Retrieve reference to a previously created container.              CloudBlobContainer container = blobClient.GetContainerReference(BlobContainerName);                container.CreateIfNotExists();               //creo se non esiste e aggiungo i permessi              BlobContainerPermissions containerPermissions = new BlobContainerPermissions();              containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;              container.SetPermissions(containerPermissions);                // Retrieve reference to a blob named "myblob".              CloudBlockBlob blockBlob = container.GetBlockBlobReference(Filename);                 blockBlob.UploadFromStream(inFileUpload);          }          catch (Exception ex)          {              return "";          }  

Answer by Albert Bori for Azure Blob 400 Bad request on Creation of container


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:

public static string ToURLSlug(this string s)  {      return Regex.Replace(s, @"[^a-z0-9]+", "-", RegexOptions.IgnoreCase)          .Trim(new char[] { '-' })          .ToLower();  }  

Then, when you try to get the container, clean it up first:

CloudBlobContainer container = blobClient.GetContainerReference(bucket.ToURLSlug());  


Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72

0 comments:

Post a Comment

Popular Posts

Powered by Blogger.