C# 为Azure应用程序服务创建blob容器

C# 为Azure应用程序服务创建blob容器,c#,azure,azure-mobile-services,azure-storage-blobs,C#,Azure,Azure Mobile Services,Azure Storage Blobs,我在将MobileService升级到Azure应用程序服务时遇到了一些问题。我有一个问题使我能够访问blob存储。现在我想将一个图像保存到blob存储中,更新后的代码如下: string cloud = ""; CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("battlecrestimage_AzureStorageConnectionSt

我在将MobileService升级到Azure应用程序服务时遇到了一些问题。我有一个问题使我能够访问blob存储。现在我想将一个图像保存到blob存储中,更新后的代码如下:

string cloud = "";

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("battlecrestimage_AzureStorageConnectionString"));
cloud = storageAccount.BlobEndpoint.ToString() + "        " + storageAccount.BlobStorageUri.ToString();

// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
if (item.ContainerName != null)
 {
     // Set the BLOB store container name on the item, which must be lowercase.
     item.ContainerName = item.ContainerName.ToLower();

     // Create a container, if it doesn't already exist.
     CloudBlobContainer container = blobClient.GetContainerReference(item.ContainerName);

     try
     {
          await container.DeleteIfExistsAsync();
          telemetry.TrackTrace("Deleted.");
     }
     catch 
     {
          telemetry.TrackTrace("Could not DeleteIfExist.");
     }
     await container.CreateIfNotExistsAsync(); //Code fails at this point

     // Create a shared access permission policy. 
     BlobContainerPermissions containerPermissions = new BlobContainerPermissions();

     // Enable anonymous read access to BLOBs.
     containerPermissions.PublicAccess = BlobContainerPublicAccessType.Blob;
     container.SetPermissions(containerPermissions);

     // Define a policy that gives write access to the container for 5 minutes.                                   
     SharedAccessBlobPolicy sasPolicy = new SharedAccessBlobPolicy()
     {
         SharedAccessExpiryTime = DateTime.UtcNow.AddMinutes(5),
         Permissions = SharedAccessBlobPermissions.Write
     };

     // Get the SAS as a string.
     item.SasQueryString = container.GetSharedAccessSignature(sasPolicy);

     // Set the URL used to store the image.
     item.ImageUri = string.Format("{0}{1}/{2}", storageAccount.BlobEndpoint,
     item.ContainerName, item.ResourceName);
}
我无法理解为什么代码在
await container.CreateIfNotExistsAsync()处失败。有人能澄清一下创建blob存储和uri的正确方法吗?将其返回给客户端

更新

以下是诊断概述中的堆栈输出

Operation=blobStorageCreationController.ExecuteAsync,Exception=Microsoft.WindowsAzure.Storage.StorageException:远程服务器返回错误:(400)错误请求。-->System.Net.WebException:远程服务器返回错误:(400)请求错误。 在c:\Program Files(x86)中的Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[T](HttpStatusCode expectedStatusCode,HttpStatusCode actualStatusCode,T retVal,StorageCommandBase 1 cmd,Exception ex)\Jenkins\workspace\release\u dotnet\u master\Lib\Common\Shared\Protocol\HttpResponseParsers.Common.cs:第50行 在c:\Program Files(x86)\Jenkins\workspace\release\u dotnet\u master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs第2620行中的Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.b_34(RESTCommand 1 cmd,HttpWebResponse resp,Exception ex,OperationContext ctx)中 在c:\Program Files(x86)\Jenkins\workspace\release\u dotnet\u master\Lib\ClassLibraryCommon\Core\Executor\Executor.Executor.EndGetResponse[T](IAsyncResult getResponseResult)中的Microsoft.WindowsAzure.Storage.Core.Executor.EndGetResponse[T](IAsyncResult getResponseResult)中:第299行 ---内部异常堆栈跟踪的结束--- 在c:\Program Files(x86)\Jenkins\workspace\release\u dotnet\u master\Lib\ClassLibraryCommon\Core\Util\StorageAsyncResult.cs中的Microsoft.WindowsAzure.Storage.Core.Util.StorageAsyncResult 1.End()处:第77行 在c:\Program Files(x86)\Jenkins\workspace\release\u dotnet\u master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs中的Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.EndCreateIfNotExists(IAsyncResult asyncResult)中 在c:\Program Files(x86)\Jenkins\workspace\release\u dotnet\u master\Lib\ClassLibraryCommon\Core\Util\AsyncExtensions.cs中的Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.c\Uuu显示Class1 1.b\Uu0(IAsyncResult ar):第66行 ---来自引发异常的上一个位置的堆栈结束跟踪--- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在System.Runtime.CompilerServices.TaskWaiter.HandleNonSuccessAndDebuggerNotification(任务任务)中 在System.Runtime.CompilerServices.TaskWaiter1.GetResult()中 在C:\BCApp\u Runtime\BCAppService\Controllers\blobStorageCreationController.d_u1.MoveNext()中的BCAppService.Controllers.blobStorageCreationController.d\uu 1.MoveNext()处:第248行


创建时不存在blob

运行此代码段时,blob容器是否已经存在


如果是这样,您必须知道删除操作不是即时操作。发出删除请求后,存储服务将表容器标记为删除,并且不再可访问。垃圾收集器稍后将删除它。如果是这种情况,您应该在标记行中获得409(冲突)错误。

运行此代码段时,blob容器是否已经存在


如果是这样,您必须知道删除操作不是即时操作。发出删除请求后,存储服务将表容器标记为删除,并且不再可访问。垃圾收集器稍后将删除它。如果是这样的话,你应该在标记行中得到409(冲突)错误。

不,这不是我在更新问题的过程中的问题。这是一个坏消息request@JTIM您能否查看异常的详细信息,特别是HttpStatusMessage,它将提供有关此400错误请求的更多详细信息。如果您使用的是Visual Studio,则可以检查异常详细信息,如。在我的例子中,容器名称中的无效字符将返回400个错误请求异常。这不是我在更新问题时的问题。这是一个坏消息request@JTIM您能否查看异常的详细信息,特别是HttpStatusMessage,它将提供有关此400错误请求的更多详细信息。如果您使用的是Visual Studio,则可以检查异常详细信息,如。在我的情况下,容器名称中的无效字符将返回一个400错误请求异常。您能说出您试图创建的容器的名称吗?请检查本文:以确定这是否是您的问题。@AlexBelotserkovskiy这是您所说的名称。升级服务后,我将Id从int改为string。字符串中有一个
\uu
,这就是问题所在。明天我将删除这个问题,因为这没有任何意义。你能说出你试图创建的容器的名称吗?检查这篇文章:看看这是否是你的问题。@AlexBelotserkovskiy正如你所说的名称。升级服务后,我将Id从int改为string。字符串中有一个
\uu
,这就是问题所在。明天我将删除这个问题,因为这没有任何意义。