指定的blob不存在 - 即使bob存在时也是如此

时间:2013-10-07 05:57:04

标签: c# azure azure-storage azure-storage-blobs azure-table-storage

使用下面的代码,我收到一条错误消息:

  

指定的blob不存在

当我调试我的代码时,在.CreateBlobContainer - 我可以看到指定的bob已创建,然后在我的代码之外我手动复制并将文本文件粘贴到我的blob中。然后当我到达最后一行时代码.DownloadToStream,它抛出异常错误,指出指定的blob不存在。 - 即使鲍勃确实存在

我的示例代码有什么问题:

        string testContainerName = "xyz"+Common.GenerateRandomEightCharString().ToLower();

        var testBlobClient = BlobClientFactory.CreateBlobClient(true);
        var testContainer = BlobClientFactory.CreateBlobContainer(testBlobClient, testContainerName);
        var zipOutputStream = new ZipOutputStream(Response.OutputStream)
        {
            CompressionLevel = CompressionLevel.Default
        };
        zipOutputStream.CompressionLevel = CompressionLevel.Default;
        zipOutputStream.EnableZip64 = Zip64Option.AsNecessary;
        CloudBlob testBlob = testBlobClient.GetBlobReference(testBlobClient.BaseUri.ToString() + testContainerName);
        zipOutputStream.PutNextEntry(testContainerName);
        BlobRequestOptions options = new BlobRequestOptions();
        options.Timeout = TimeSpan.FromSeconds(20.0);
        testBlob.DownloadToStream(zipOutputStream, options); //Exception error here

这是异常消息。

Microsoft.WindowsAzure.StorageClient.StorageClientException was unhandled by user code
  HResult=-2146233088
  Message=The specified blob does not exist.
  Source=Microsoft.WindowsAzure.StorageClient
  StackTrace:
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.get_Result()
       at Microsoft.WindowsAzure.StorageClient.Tasks.Task`1.Execute()
       at Microsoft.WindowsAzure.StorageClient.RequestWithRetry.RequestWithRetrySyncImpl[TResult](ShouldRetry retryOracle, SynchronousTask`1 syncTask)
       at Microsoft.WindowsAzure.StorageClient.TaskImplHelper.ExecuteSyncTaskWithRetry[TResult](SynchronousTask`1 syncTask, RetryPolicy policy)
       at Microsoft.WindowsAzure.StorageClient.CloudBlob.DownloadToStream(Stream target, BlobRequestOptions options)
       at ............................
       at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters)
       at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters)
       at System.Web.Mvc.ControllerActionInvoker.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12()
       at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation)
  InnerException: System.Net.WebException
       HResult=-2146233079
       Message=The remote server returned an error: (404) Not Found.
       Source=System
       StackTrace:
            at System.Net.HttpWebRequest.GetResponse()
            at Microsoft.WindowsAzure.StorageClient.EventHelper.ProcessWebResponseSync(WebRequest req, EventHandler`1 handler, Object sender)
       InnerException: 

2 个答案:

答案 0 :(得分:1)

我刚刚重启了我的webrole并开始工作

答案 1 :(得分:0)

根据您的评论,我看到发生了什么。基本上,您通过代码创建一个blob容器(将其视为文件夹),然后手动复制该容器中的文件。但是,您用于blob的URL(将其视为文件)是容器的URL而不是文件。

您需要做的是将文件的名称附加到blob URL。因此,假设您在容器中手动复制的文件名为xyz.txt,您将使用以下代码创建testBlob对象:

CloudBlob testBlob = testBlobClient.GetBlobReference(testBlobClient.BaseUri.ToString() + testContainerName + "/xyz.txt");

试一试。它应该工作。