Azure Blob 400 Bad request on Creation of container

后端 未结 12 2466
夕颜
夕颜 2020-12-15 16:26

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 storag         


        
相关标签:
12条回答
  • 2020-12-15 16:52

    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.
    0 讨论(0)
  • 2020-12-15 16:52

    Mine was a stupid naming problem! Apparently we are not allowed to use uppercase in the names.

    I've just changed this:

    CloudBlobContainer container = blobClient.GetContainerReference("MyContainer");
    container.CreateIfNotExists();
    

    To

    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    container.CreateIfNotExists();
    
    0 讨论(0)
  • 2020-12-15 16:53

    I encountered this error after updating the packages but not my code. My issue is that the connection string format and content has changed since I first started using Azure Storage several years ago. Make sure to update your connection string appropriately from the access keys options within the azure portal.

    In my case: I was missing this in my connection string: EndpointSuffix=core.windows.net

    0 讨论(0)
  • 2020-12-15 16:58

    From experimentation it appears as though container names must always also be lower case. There must be an implicit conversion internally, which causes it to create the original blob in lower case, but not when it compares it in createifnotexists(async). But when it goes to re-create it, it lower cases it again, which results in a conflict. This is a best guess.

    0 讨论(0)
  • 2020-12-15 17:00

    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());
    
    0 讨论(0)
  • 2020-12-15 17:01

    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)?

    0 讨论(0)
提交回复
热议问题