Search code examples
c#azureazure-storageazure-functions

Azure Function Access CloudBlobContainer from HttpTriggerFunction


Very, very new to Azure Functions and getting very frustrated.

All I want to do is execute on a 'get' request from a HttpTriggerFunction and return stream content from the CloudBlobContainer.

I really don't see why this is so hard. Just trying to host a SPA using Azure Functions.

Something like this

  public static class UIHandler
{
    [FunctionName("UIHandler")]
    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = null)]HttpRequest req, 
        TraceWriter log, 
        CloudBlobContainer container)
    {
        log.Info("C# HTTP trigger function processed a request.");
        var stream = await container.GetBlockBlobReference({Infer file name from request here}).OpenReadAsync();

        return new HttpResponseMessage()
        {
            StatusCode = HttpStatusCode.OK,
            Content = new StreamContent(stream)
        };

    }
}

When I try to run this I get the following error.

Run: Microsoft.Azure.WebJobs.Host: Error indexing method 'UIHandler.Run'. Microsoft.Azure.WebJobs.Host: Cannot bind parameter 'container' to type CloudBlobContainer. Make sure the parameter Type is supported by the binding. If you're using binding extensions (e.g. ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. config.UseServiceBus(), config.UseTimers(), etc.).

I'm using Azure Functions 2. I can't see from the web how to setup the browsing extensions for this. Iv'e also looked into Input and Output bindings. I don't understand what makes a parameter input or output bound when your using C# that only seems to exist in the JSON.

Do I need to corresponding JSON file ? If so what is it called where does it go.

Thanks in Advance


Solution

  • Have a look at Blob Storage Input Binding. The very first sample there shows how to read blob stream, just replace Queue Trigger with HTTP trigger, e.g.

    [FunctionName("UIHandler")]
    public static async Task<HttpResponseMessage> Run(
        [HttpTrigger(AuthorizationLevel.Function, "get", Route = "{name}")] HttpRequest req,
        string name, 
        TraceWriter log, 
        [Blob("samples-workitems/{name}", FileAccess.Read)] Stream stream)
    {
        log.Info($"C# HTTP trigger function processed a request for {name}.");
    
        return new HttpResponseMessage()
        {
            StatusCode = HttpStatusCode.OK,
            Content = new StreamContent(stream)
        };
    
    }