Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/27.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 4 Azure Blob 400创建容器时的错误请求_Asp.net Mvc 4_Azure_Azure Storage_Azure Storage Blobs_Blobstorage - Fatal编程技术网

Asp.net mvc 4 Azure Blob 400创建容器时的错误请求

Asp.net mvc 4 Azure Blob 400创建容器时的错误请求,asp.net-mvc-4,azure,azure-storage,azure-storage-blobs,blobstorage,Asp.net Mvc 4,Azure,Azure Storage,Azure Storage Blobs,Blobstorage,我正在开发一个ASP.NETMVC4应用程序,我正在使用Azure Blob来存储我的用户将要上传的图像。我有以下代码: var storageAccount = CloudStorageAccount.Parse(ConfigurationManager.ConnectionStrings["StorageConnection"].ConnectionString); var blobStorage = storageAccount.CreateCloudBlobClient(); /

我正在开发一个ASP.NETMVC4应用程序,我正在使用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

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

更新:
我已将容器名称更改为just
image
,但仍然得到相同的异常。另外,开发连接字符串如下:

正如您通过研究发现的,问题在于名称

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


有关更多信息,请参见容器名称下的#3,网址:

3.容器名称中的所有字母必须为小写。
我试图复制您的问题,但看起来您使用的是较旧版本的客户端库,因为
container.CreateIfNotExist()
现在是
container.CreateIfNotExists()
。您是否考虑过将客户端升级到最新版本(2.1)?

我最终发现了问题


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

有必要在异常中查看httpstatusmessage: 就我而言,这个错误是因为 请求的URI不表示服务器上的任何资源

因此,我发现我的
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 "";
        }

为了扩展@kwill的答案,我实现了一个解决方案,用于将任何字符串转换为可接受的容器名称,基于:

然后,当您尝试获取容器时,请先将其清理干净:

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

确保您的存储库和存储仿真程序版本没有“不同步”。我更新了我的库,但没有将emulator更新到最新版本,因此得到了这个确切的情况。

从实验来看,似乎容器名称也必须始终为小写。必须在内部进行隐式转换,这会导致它以小写形式创建原始blob,但在createifnotexists(异步)中进行比较时不会这样做。但当它去重新创建它时,它会再次降低它的案例数,从而导致冲突。这是最好的猜测。

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


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

如果您刚刚更新了WindowsAzure.Storagenuget软件包,并且您的应用程序开始因http错误400错误请求而崩溃

在我的例子中,它发生在我更新到8.2.1时,我的本地模拟器是5.1版

我的解决办法是:

  • 转到Microsoft Azure SDK页面
  • 搜索“Azure存储模拟器”并下载最新的存储模拟器。通常在“命令行工具”部分的页面中间左侧
  • 安装最新的仿真器
  • 你可以走了
  • 当我下载Storage Emulator 5.2并从5.1升级时,错误停止了。 这样的错误在我身上已经发生过好几次了

    -请添加对开发模式的检查,并抛出一个有意义的异常,消息如下-“您已安装Azure Storage Emulator版本X.Y.Z。要使用Azure Emulator的当前WindowsAzure.Storage库**V.V.V,您需要从此链接安装模拟器版本Z.Z.Z”**或任何你认为有用的东西。< /P>
    这种问题已经浪费了我好几个小时的时间,我想世界各地成千上万的开发人员也遇到了同样的问题,但这个例外仍然存在——已经持续了4年多

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

    我刚刚改变了这一点:

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

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

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

    在我的例子中:我的连接字符串中缺少以下内容:
    EndpointSuffix=core.windows.net

    解决此问题的简单方法是,容器应始终使用小写。我也遇到了同样的问题,在将容器名称更改为小写形式的all之后,问题得到了解决。

    Hi,感谢您的回复。该语句末尾有一个
    .ToLower()
    ,它将整个字符串转换为小写。在这两种情况下,我都认为我可能错过了什么,于是我走了一个头,把名字改成了static
    image<
    
    CloudBlobContainer container = blobClient.GetContainerReference("MyContainer");
    container.CreateIfNotExists();
    
    CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");
    container.CreateIfNotExists();