Terje Nygård
Terje Nygård

Reputation: 1257

Azure CloudBlockBlob. Cannot find the blob when DownloadToStream. Uri seems repeated?

I'm using Azure Development Blob Storage..

Uploading the blob works perfectly, and i can double-click the image uploaded in the visual studio blob container view and open the picture...

But if you look at this picture :

enter image description here

...there is something wrong with the Uri ?

Here is the code i'm using :

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;

Here is how i store the blobs :

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

        blockBlob.UploadFromStream(fileStream);
        return blockBlob;
    }

Here is how i get the 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;
    }

Here is how the blob look like in the dev. storage (it's clickable and viewable from there)

    Name : bla-bla-bla.jpg
    Content Type : application/octet-stream
    URL : http://127.0.0.1:10000/devstoreaccount1/userid1/bla-bla-bla.jpg

So... what should i change in the way i get the picture to get the normal URL ?

Soo.. is there any obvious thing's i'm doing wrong here ?

here is the complete NET Response msg :

{"$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)"}}

Upvotes: 7

Views: 9466

Answers (1)

Gaurav Mantri
Gaurav Mantri

Reputation: 136196

It seems that you are storing blob's absolute URL in BlobUrl property in your application. Based on the documentation for CloudBlobContainer.GetBlockBlobReference, it should be name of the blob. So in your case, it should be just the name of the image file.

If you look closely at the screenshot you shared especially blob's URI property, you will notice the problem. Blob's Uri property is http://127.0.0.1:10000/devstoreaccount1/userid1/http://127.0.0.1:10000/devstoreaccount1/userid1/85066...mongo.jpg instead of http://127.0.0.1:10000/devstoreaccount1/userid1/85066...mongo.jpg.

Upvotes: 12

Related Questions