Azure Blob 400关于创建容器的错误请求

问题描述 投票:34回答:12

我正在开发一个ASP.Net MVC 4应用程序,我正在使用Azure Blob来存储我的用户要上传的图像。我有以下代码:

 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
    } 

一旦执行if语句,我就会收到以下异常:

  {"The remote server returned an error: (400) Bad Request."}

我以为这是容器的名称,但我没有看到任何错误。连接字符串似乎创建了一个包含blob所有详细信息的良好存储空间。我不知所措。我研究了网络,每个人都说这是一个命名问题,但我发现它没有任何问题。

我使用的测试容器名称:imageads-57905553-8585-4d7c-8270-be9e611eda81

Container有以下uri:{http://127.0.0.1:10000/devstoreaccount1/imageads-57905553-8585-4d7c-8270-be9e611eda81}

更新:我已将容器名称更改为image,我仍然得到相同的异常。开发连接字符串如下:<add name="StorageConnection" connectionString="UseDevelopmentStorage=true" />

asp.net-mvc-4 azure azure-storage azure-storage-blobs blobstorage
12个回答
88
投票

正如您在研究中发现的那样,问题就在于名称。

你说你的测试容器名为imageads-57905553-8585-4d7c-8270-be9e611eda81,但在你的代码中你使用的是ImageAds-57905553-8585-4d7c-8270-be9e611eda81。注意资本化的差异。如果将容器名称切换为全部小写,它将正常工作。

有关更多信息,请参阅Naming and Referencing Containers, Blobs, and Metadata上Container Names下的#3:

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

1
投票

我尝试重现你的问题,但看起来你正在使用旧版本的客户端库,因为container.CreateIfNotExist()现在是container.CreateIfNotExists()。您是否考虑过升级最新的客户端版本(2.1)?


1
投票

在异常中查看httpstatusmessage是必要的:在我的情况下,错误是因为请求的URI不代表服务器上的任何资源。

所以我看到我的BlobContainerName不包含正确的容器(或不存在)

CloudBlobContainer container = > blobClient.GetContainerReference(BlobContainerName);

我见过的另一种情况是容器名称错误。 blobcontainername,必须是“mycontainer1”或“mycontainer2”等名称,依此类推

这里是添加容器的代码

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 "";
        }

1
投票

解决这个问题的简单方法是,容器应始终为小写。我有同样的问题,在将容器名称更改为小写的所有内容后得到解决。


11
投票

为了扩展@kwill的答案,我实现了一个解决方案,用于将任何字符串转换为可接受的容器名称,基于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();
}

然后,当您尝试获取容器时,请先清理它:

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

7
投票

我实际上最终找到了问题。

我的问题是blob存储模拟器无法启动(其他模拟器将启动,我错过了blob)。问题最终是端口10000(默认blob仿真器端口)已被另一个软件使用。我使用Netstat cmd工具查看它是哪个软件,将其杀死,它现在像魅力一样工作!感谢大家!!


5
投票

确保您的存储库和存储模拟器版本不“不同步”。我更新了我的库,但没有将模拟器更新到最新版本并得到了这个确切的情况。


4
投票

如果您刚刚更新了WindowsAzure.Storage nuget包,并且您的应用程序开始崩溃,并出现http错误400错误请求:

在我的情况下,它发生在我更新到8.2.1并且我的本地模拟器是版本5.1。

我的解决方案是:

  1. 转到Microsoft Azure SDK页面here.
  2. 搜索“Azure存储模拟器”并下载最新的存储模拟器。通常位于“命令行工具”部分左侧页面的中间位置
  3. 安装最新的模拟器
  4. 你已准备好出发。

当我下载Storage Emulator 5.2并从5.1升级时,错误停止了。这样的错误已经好几次发生在我身上了。

My humble request if anybody from Microsoft Azure Storage Emulator team reads this - 请添加一个开发模式检查,并抛出一个有意义的例外,例如“你安装了Azure存储模拟器版本XYZ。为了使用当前的WindowsAzure.Storage库** VVV和Azure模拟器,你需要安装版本来自此链接的模拟器的ZZZ“。**或您认为有用的任何内容。

这种问题浪费了我几个小时的时间,我想全世界成千上万的开发人员也发生了同样的事情,而且这个例外仍然存在 - 超过4年!


3
投票

我刚刚解决了这个问题并修复了它。

我的容器名称很好,但我不小心将连接字符串中的AccountName参数大写。这导致了我的400。


3
投票

我是一个愚蠢的命名问题!显然我们不允许在名称中使用大写字母。

我刚刚改变了这个:

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

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

2
投票

从实验来看,似乎容器名称也必须始终为小写。内部必须有隐式转换,这会导致它以小写形式创建原始blob,但在createifnotexists(async)中比较它时则不会。但是当它重新创建它时,它再次降低它的情况,这导致冲突。这是最好的猜测。


2
投票

我在更新软件包后遇到此错误,但不是我的代码。我的问题是,自从几年前我第一次开始使用Azure存储以来,连接字符串格式和内容已经发生了变化。确保从azure门户中的访问键选项中适当更新连接字符串。

在我的情况下:我在我的连接字符串中遗漏了这个:EndpointSuffix = core.windows.net

© www.soinside.com 2019 - 2024. All rights reserved.