Adventures with Azure Storage: Read/Write Files to Blob Storage from Azure Functions

azure storage

In my last article, Adventures with Azure Storage: Read/Write Files to Blob Storage from a .NET Core Web API, we looked at uploading and downloading files from Azure Blob Storage using a .NET Core Web API, in this article, we are going to perform the same task, but this time, we will use Azure Functions in place of the .NET Core Web API.

Create an Azure Storage account or use an existing one.

Open Visual Studio or VS Code and Create a new Azure Function project.

Add a folder called Helpers.

Add two files, one called AzureStorageBlobOptions.cs and another called AzureStorageBlobOptionsTokenGenerator.cs.

Configuration will be provided by AzureStorageBlobOptions and the SAS Token will be generated by AzureStorageBlobOptionsTokenGenerator.

The AzureStorageBlobOptions gets tweaked to pull configuration from environment variables.

In the local.settings.json,  Add configuration keys and Update the key values based on your Azure Storage account and Blob container.

Note, I also added the CORS option so I would not run into any issues when working locally.

Add a class called Startup.cs.

Add the NuGet package for Microsoft.Azure.Functions.Extensions.

In the Startup.cs file we will add dependency injection support for the AzureStorageBlobOptions and AzureStorageBlobOptionsTokenGenerator classes.

Add a new HttpTrigger function called FileUploadHttpTrigger.cs.

Run the Azure Function and open up Postman.

Create a new POST request.

Enter the API URL, in my case it was https://localhost:7071/api/files.

Set Body to form-data.

Add key called file, make sure to change the type to File, defaults to Text.

Add the file to upload and Send the request.

If the call is successful, you should receive an OK response with the new name of the file uploaded.

Not the name property, we will need to remember that to test the download funciton.

Add a new HttpTrigger function called FileDownloadHttpTrigger.cs.

Run the Azure Function and open up Postman.

Create a new GET request in Postman.

Enter the API URL, in my case it was https://localhost:7071/api/files/RETURN_FILE_NAME, and Send the request.

Replace RETURN_FILE_NAME with the name of the file that returned in the previous step where we uploaded the file.

If the call is successful, you should see the image displayed in Postman.

Some final comments.

I thought I could write to Azure Blob Storage using the output bindings, but could not figure out how to set the name of the Blob, so opted to write to the Azure Blob Storage myself.

The code could be more readable and easier to maintain if I refactored the Blob commands to a common helper library.

Thanks for reading!


Discover more from Matt Ruma

Subscribe to get the latest posts sent to your email.

9 Replies to “Adventures with Azure Storage: Read/Write Files to Blob Storage from Azure Functions”

  1. Hello Matt,

    This is Mani from India. I am trying to accomplish the same with Azure Functions but I am getting an error when registering the helper classes. Can you please share the completed Sample project (through GitHub/OneDrive/Email or so) which will help to understand.
    Thanks a lot for sharing!

  2. Hi, very nice, thanks!

    Your Github repo is different than de example here.
    From wish package IOptions came from? (Also not found in the repo)

  3. Can you show what the result of this step would produce?

    In the Startup.cs file we will add dependency injection support for the AzureStorageBlobOptions and AzureStorageBlobOptionsTokenGenerator classes.

    builder.Services.AddSingleton();

    builder.Services.AddSingleton();

Leave a Reply

Your email address will not be published. Required fields are marked *