为Azure App Service创建blob容器

时间:2016-05-05 12:13:04

标签: c# azure azure-mobile-services azure-storage-blobs

我在将MobileService升级到Azure App Service时遇到了一些问题。我有以下问题Blob storage access from Azure App Service,这使我能够访问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)错误请求。      在Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException [T](HttpStatusCode expectedStatusCode,HttpStatusCode actualStatusCode,T retVal,StorageCommandBase 1 cmd,Exception ex)中c:\ Program Files(x86)\ Jenkins \ workspace \ release_dotnet_master \ Lib \ Common \ Shared \ Protocol \ HttpResponseParsers.Common.cs:第50行      在Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.b__34(RESTCommand 1 cmd,HttpWebResponse resp,Exception ex,OperationContext ctx)中的c:\ Program Files(x86)\ Jenkins \ workspace \ release_dotnet_master \ Lib \ ClassLibraryCommon \ Blob \ CloudBlobContainer。 cs:2620行      在Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse [T](IAsyncResult getResponseResult)中的c:\ Program Files(x86)\ Jenkins \ workspace \ release_dotnet_master \ Lib \ ClassLibraryCommon \ Core \ Executor \ Executor.cs:line 299      ---内部异常堆栈跟踪结束---      在Microsoft.WindowsAzure.Storage.Core.Util.StorageAsyncResult 1.End()在c:\ Program Files(x86)\ Jenkins \ workspace \ release_dotnet_master \ Lib \ ClassLibraryCommon \ Core \ Util \ StorageAsyncResult.cs:第77行      在c:\ Program Files(x86)\ Jenkins \ workspace \ release_dotnet_master \ Lib \ ClassLibraryCommon \ Blob \ CloudBlobContainer.cs中的Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.EndCreateIfNotExists(IAsyncResult asyncResult):第381行      在Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions。<> c__DisplayClass1 1.b__0(IAsyncResult ar)在c:\ Program Files(x86)\ Jenkins \ workspace \ release_dotnet_master \ Lib \ ClassLibraryCommon \ Core \ Util \ AsyncExtensions中.cs:第66行   ---从抛出异常的先前位置开始的堆栈跟踪结束---      在System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)      在System.Runtime.CompilerServices.TaskAwaiter1.GetResult()      在C:\ BCApp_Runtime \ BCAppService \ Controllers \ blobStorageController.cs中的BCAppService.Controllers.blobStorageCreationController.d__1.MoveNext():第248行

创建时不存在blob。

1 个答案:

答案 0 :(得分:2)

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

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