Azure CloudBlockBlob.在DownloadToStream中找不到blob.Uri似乎重复了?

Ter*_*ård 7 c# azure azure-storage azure-storage-blobs

我正在使用Azure开发Blob存储..

上传blob工作得很好,我可以双击visual studio blob容器视图中上传的图像并打开图片...

但如果你看看这张照片:

在此输入图像描述

...... Uri有问题吗?

这是我正在使用的代码:

public MemoryStream DownloadBlob(int id)
    {
        Photo photo = PhotoServices.GetPhotoById(id);
        var cloudBlobContainer = _blobClient.GetContainerReference(CurrentBlobContainerName);
        var blob = cloudBlobContainer.GetBlockBlobReference(photo.BlobUrl);
        var memorystream = new MemoryStream();

// THIS LINE GIVES BLOB NOT FOUND EXCEPTION
        blob.DownloadToStream(memorystream);

       memorystream.Position = 0;
       return memorystream;
Run Code Online (Sandbox Code Playgroud)

这是我如何存储blob:

public CloudBlockBlob UploadBlob(Stream fileStream, string fileName)
    {
        var blobName = Guid.NewGuid() + fileName;
        var blockBlob = GetContainer().GetBlockBlobReference(blobName);

        blockBlob.UploadFromStream(fileStream);
        return blockBlob;
    }
Run Code Online (Sandbox Code Playgroud)

这是我如何得到blob:

public MemoryStream DownloadBlob(int id)
    {
        Photo photo = PhotoServices.GetPhotoById(id);
        var cloudBlobContainer = _blobClient.GetContainerReference(CurrentBlobContainerName);
        var blob = cloudBlobContainer.GetBlockBlobReference(photo.BlobUrl);
        var memorystream = new MemoryStream();
        memorystream.Position = 0;
        blob.DownloadToStream(memorystream);
        return memorystream;
    }
Run Code Online (Sandbox Code Playgroud)

这是blob中blob的样子.存储(可点击并可从那里查看)

    Name : bla-bla-bla.jpg
    Content Type : application/octet-stream
    URL : http://127.0.0.1:10000/devstoreaccount1/userid1/bla-bla-bla.jpg
Run Code Online (Sandbox Code Playgroud)

那么......我应该如何改变我获得正常URL的图片?

Soo ..有没有明显的事我在这里做错了?

这是完整的NET响应消息:

{"$id":"1","Message":"An error has occurred.","ExceptionMessage":"The remote server returned an error: (404) Not Found.","ExceptionType":"Microsoft.WindowsAzure.Storage.StorageException","StackTrace":"   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)\r\n   at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.DownloadRangeToStream(Stream target, Nullable`1 offset, Nullable`1 length, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)\r\n   at Microsoft.WindowsAzure.Storage.Blob.CloudBlockBlob.DownloadToStream(Stream target, AccessCondition accessCondition, BlobRequestOptions options, OperationContext operationContext)\r\n   at ServiceLibrary.Services.StorageServices.DownloadBlob(Int32 id) in c:\\PhotoApp\\ServiceLibrary\\Services\\StorageServices.cs:line 116\r\n   at PhotoWebApp.Controllers.PhotoSubmitController.GetPhotoById(Int32 id) in c:\\PhotoApp\\PhotoWebApp\\Controllers\\PhotoSubmitController.cs:line 28\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"$id":"2","Message":"An error has occurred.","ExceptionMessage":"The remote server returned an error: (404) Not Found.","ExceptionType":"System.Net.WebException","StackTrace":"   at System.Net.HttpWebRequest.GetResponse()\r\n   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd, IRetryPolicy policy, OperationContext operationContext)"}}
Run Code Online (Sandbox Code Playgroud)

Gau*_*tri 12

您似乎BlobUrl在应用程序中将blob的绝对URL存储在属性中.根据文档CloudBlobContainer.GetBlockBlobReference,它应该是name of the blob.所以在你的情况下,它应该只是图像文件的名称.

如果仔细查看您共享的屏幕截图,特别是blob的URI属性,您会发现问题.Blob的Uri属性http://127.0.0.1:10000/devstoreaccount1/userid1/http://127.0.0.1:10000/devstoreaccount1/userid1/85066...mongo.jpg不是http://127.0.0.1:10000/devstoreaccount1/userid1/85066...mongo.jpg.