3

I'm triggering an Azure Function with HTTP POST and would like to save the request body to a Blob. Per the documentation this should be relatively straight forward using the output Blob storage binding. However, I was not able to get it to work. When I check the auto-generated function.json, I notice there is no binding created for the output.

The following function is working, but I would like to know what I am missing regarding the Blob output binding. How would you go about changing this to use Blob output binding?

public static class SaveSubContent
{
    [FunctionName("SaveSubContent")]
    public static IActionResult Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "post", Route = null)] HttpRequest req,
        ILogger log, ExecutionContext context
        )
    {
        var config = new ConfigurationBuilder()
                        .SetBasePath(context.FunctionAppDirectory)
                        .AddJsonFile("local.settings.json", optional: true, reloadOnChange: true)
                        .AddEnvironmentVariables()
                        .Build();

        var connectionString = config["AzureWebJobsStorage"];

        string containerName = "france-msgs";
        string blobName = Guid.NewGuid().ToString() + ".json";


        BlobContainerClient container = new BlobContainerClient(connectionString, containerName);
        container.CreateIfNotExists();

        BlobClient blob = container.GetBlobClient(blobName);

        blob.Upload(req.Body);

        log.LogInformation("Completed Uploading: " + blobName);
        return new OkObjectResult("");
    }
}

1 Answer 1

3

You can simplify this quite a bit using the output binding in conjunction with the {rand-guid} special binding expression. {rand-guid} will generate a guid for you as part of the binding, so you don't have to do it in code.

public static class Function1
{
    [FunctionName("Function1")]
    public static async Task<IActionResult> Run(
        [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = null)] HttpRequest req,
        [Blob("france-msgs/{rand-guid}.json", FileAccess.ReadWrite, Connection = "AzureWebJobsStorage")] CloudBlockBlob outputBlob,
        ILogger log)
    {
        string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
        await outputBlob.UploadTextAsync(requestBody);

        return new OkObjectResult("");
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much! I had a feeling there was part of the documentation I was missing and the {rand-guid} part helped me get to this: learn.microsoft.com/en-us/azure/azure-functions/… The binding expressions make much more sense now
I found this rather interesting: learn.microsoft.com/en-us/azure/azure-functions/… as I needed to create the name dynamically.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.