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

前端之家收集整理的这篇文章主要介绍了c# – Azure CloudBlockBlob.在DownloadToStream中找不到blob. Uri似乎重复了?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在使用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;

这是我如何存储blob:

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

        blockBlob.UploadFromStream(fileStream);
        return blockBlob;
    }

这是我如何得到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;
    }

这是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

那么……我应该如何改变我获得正常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,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","ExceptionType":"System.Net.WebException","StackTrace":"   at System.Net.HttpWebRequest.GetResponse()\r\n   at Microsoft.WindowsAzure.Storage.Core.Executor.Executor.ExecuteSync[T](RESTCommand`1 cmd,OperationContext operationContext)"}}

解决方法

您似乎在应用程序中的BlobUrl属性中存储blob的绝对URL.根据 CloudBlobContainer.GetBlockBlobReference的文档,它应该是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.

猜你在找的C#相关文章