2016-05-05 19 views
0

我在将MobileService升级到Azure应用服务时遇到了一些问题。我有以下问题Blob storage access from Azure App Service,这使我能够访问blob存储。现在我想将图像保存到Blob存储,更新后的代码是这样的:为Azure应用服务创建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: The remote server returned an error: (400) Bad Request. ---> System.Net.WebException: The remote server returned an error: (400) Bad Request. at Microsoft.WindowsAzure.Storage.Shared.Protocol.HttpResponseParsers.ProcessExpectedStatusCodeNoException[T](HttpStatusCode expectedStatusCode, HttpStatusCode actualStatusCode, T retVal, StorageCommandBase 1 cmd, Exception ex) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\Common\Shared\Protocol\HttpResponseParsers.Common.cs:line 50 at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.b__34(RESTCommand 1 cmd, HttpWebResponse resp, Exception ex, OperationContext ctx) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs:line 2620 at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.EndGetResponse[T](IAsyncResult getResponseResult) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Executor\Executor.cs:line 299 --- End of inner exception stack trace --- at Microsoft.WindowsAzure.Storage.Core.Util.StorageAsyncResult 1.End() in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Util\StorageAsyncResult.cs:line 77 at Microsoft.WindowsAzure.Storage.Blob.CloudBlobContainer.EndCreateIfNotExists(IAsyncResult asyncResult) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Blob\CloudBlobContainer.cs:line 381 at Microsoft.WindowsAzure.Storage.Core.Util.AsyncExtensions.<>c__DisplayClass1 1.b__0(IAsyncResult ar) in c:\Program Files (x86)\Jenkins\workspace\release_dotnet_master\Lib\ClassLibraryCommon\Core\Util\AsyncExtensions.cs:line 66 --- End of stack trace from previous location where exception was thrown --- at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) at System.Runtime.CompilerServices.TaskAwaiter1.GetResult() at BCAppService.Controllers.blobStorageCreationController.d__1.MoveNext() in C:\BCApp_Runtime\BCAppService\Controllers\blobStorageController.cs:line 248

在创建时不存在blob。

+0

你可以说你试图创建的容器的名称吗? –

+1

检查这篇文章:http://stackoverflow.com/questions/19599819/azure-blob-400-bad-request-on-creattion-of-container看看是否是你的问题。 –

+0

@AlexBelotserkovskiy这是你说的名字。在升级服务之后,我将Id从int更改为字符串。该字符串有一个'_',这是问题。我明天将删除这个问题,因为这没有任何贡献。 – JTIM

回答

2

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

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

+0

不,这不是我正在更新问题的问题。这是一个400错误的请求 – JTIM

+0

@JTIM你可以检查异常的细节特别** HttpStatusMessage **,它将提供有关这个400错误请求的更多细节。如果您使用Visual Studio,则可以检查异常详细信息,如[this](http://i.stack.imgur.com/hcBoI.png)。在我的情况下,容器名称中的无效字符将返回一个400错误的请求异常。 – forester123

相关问题