Serving images from Azure Blob Storage in dot net core

3.1k views Asked by At

I am attempting to write some Middleware to serve Azure Blobs via proxy.  The handler is being called, the blob is being retrieved, but my image is not being displayed.

I wrote a service to connect to the Storage Account and create a Blob Client. I wrote middleware that consumes the service and then downloads the requested blob and writes it to the Response. Normally, I would expect to download the blob as a byte array or a stream and write it to the OutputStream and that does not seem to be an option using the new httpContext in .net core.

My Middleware:

namespace SampleApp1.WebApp.Middleware
{
    public class BlobFileViewHandler
    {
        public BlobFileViewHandler(RequestDelegate next)
        {
        }

        public async Task Invoke(HttpContext httpContext, IBlobService svc)
        {
            string container = httpContext.Request.Query["container"];
            string itemPath = httpContext.Request.Query["path"];

            Blob cbb = await svc.GetBlobAsync(container, itemPath);

            httpContext.Response.ContentType = cbb.ContentType;
            await httpContext.Response.Body.WriteAsync(cbb.Contents, 0, cbb.Contents.Length);            
        }
    }

    // Extension method used to add the middleware to the HTTP request pipeline.
    public static class BlobFileViewHandlerExtensions
    {
        public static IApplicationBuilder UseBlobFileViewHandler(this IApplicationBuilder builder)
        {
            return builder.UseMiddleware<BlobFileViewHandler>();
        }
    }
}

I call the middleware using the Map function in Startup as below:

app.Map(new PathString("/thumbs"), a => a.UseBlobFileHandler());

And finally, I attempt to use that handler on a test page as follows:

    <img src="~/thumbs?qs=1" alt="thumbtest" />

When I debug I can see all the correct parts being hit, but the image never loads, I just get the following:

broken image

I feel like I'm missing something simple, but I'm not sure what that is. I am using NetCoreApp Version 1.1.

1

There are 1 answers

0
randcd On BEST ANSWER

I guess I jumped the gun a little early, because it appears you CAN write to the OutputStream, it's just referenced a little differently. Below is the working implementation of what I was attempting in the middleware:

public class BlobFileHandler
{
    public BlobFileHandler(RequestDelegate next)
    {
    }

    public async Task Invoke(HttpContext httpContext)
    {
        string container = "<static container reference>";
        string itemPath = "<static blob reference>";
        //string response;
        IBlobService svc = (IBlobService)httpContext.RequestServices.GetService(typeof(IBlobService));

        CloudBlockBlob cbb = svc.GetBlob(container, itemPath);

        httpContext.Response.ContentType = "image/jpeg";//cbb.Properties.ContentType;            
        await cbb.DownloadToStreamAsync(httpContext.Response.Body);
    }
}