Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Azure by (45.3k points)

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 the statement is executed 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 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 URL: {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" />

1 Answer

0 votes
by (16.8k points)

All the letter for container name, it must be a lowercase.

Otherwise, try this method for implementing a solution for converting any string into an acceptable container name:

public static string ToURLSlug(this string s)

{

    return Regex.Replace(s, @"[^a-z0-9]+", "-", RegexOptions.IgnoreCase)

        .Trim(new char[] { '-' })

        .ToLower();

}

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

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

Browse Categories

...