ΩmegaMan
ΩmegaMan

Reputation: 31576

Unable to Stream Picture from Azure Blob

I have a rest endpoint which I can't seem to stream out a picture from a valid Azure Blob. When I look at the stream in debug it has the appropriate byte sizes and appears to be healthy.

The following code works (loading of Azure creds/container not shown for brevity), but fails on output return (See 500 below)

if (await blockBlob.ExistsAsync())
{
     var stream = new MemoryStream();

     await blockBlob.DownloadToStreamAsync(stream);

    return new FileStreamResult(stream, "image/jpeg"); 

Result 500 (Error: Internal Server Error):

server: Kestrel    
x-sourcefiles: =?UTF-8BQzpcX0NvZGVcUGV0U21hcnRcUG9kc1xQT0RTXGN1c3RvbWVyc1xwaG90b3NcZG93bmxvYWQ=?= 
x-powered-by: ASP.NET   
date: Fri, 01 Jun 2018 21:21:18 GMT   
content-length: 0

Endpoint

[HttpGet]
[Route("/photos/download")]
[SwaggerOperation("Photo Download")]
public async Task<IActionResult> DownloadPhoto([FromQuery]long? photoId)

This hard coded return works like a charm

var image = System.IO.File.OpenRead(@"C:\Temp\info.png");
return File(image, "image/jpeg");

I can view and download the named blob in Azure Storage Explorer.

Upvotes: 1

Views: 607

Answers (1)

Jerry Liu
Jerry Liu

Reputation: 17790

It seems you forgot to seek the stream position back to 0.

Just add stream.Position = 0; after downloading the stream.

Upvotes: 3

Related Questions