gpt4 book ai didi

c# - Azure CloudBlockBlob。 DownloadToStream 时找不到 blob。 Uri 似乎重复了?

转载 作者:太空狗 更新时间:2023-10-29 20:04:38 26 4
gpt4 key购买 nike

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

上传 Blob 效果非常好,我可以在 Visual Studio Blob 容器 View 中双击上传的图像并打开图片...

但是如果你看看这张图片:

enter image description here

...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 在开发中的样子。存储(可以点击并从那里查看)

    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, 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)"}}

最佳答案

您似乎将 blob 的绝对 URL 存储在应用程序的 BlobUrl 属性中。基于 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# - Azure CloudBlockBlob。 DownloadToStream 时找不到 blob。 Uri 似乎重复了?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24986336/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com