How to Retrieve a File from Azure Blob Storage with an Azure Function (2023)

By: Koen Verbeeck | Updated: 2023-01-30 | Comments (1) | Related: > Azure Functions


Problem

I need to read a file stored in an Azure Blob Storage container. However, I cannotuse Azure Data Factory (ADF) because I need to do some transformations on the data.Can I achieve my goal with Azure Functions instead?

Solution

There are several different methods for reading files in the Azure cloud. Theeasiest options don't require code: a Copy Data activity in ADF or an AzureLogic App. However, both copy the data to a destination. What if we want to do morecomplex transformations? Another option is dataflows in ADF (both mapping dataflowsor Power Query), but these might be a bit too expensive to process small files.

A solution that involves a bit more code is Azure Functions. Using C# libraries,we can read a file from Azure Blob Storage into memory and then apply the transformationswe want. In this tip, we'll cover a solution that retrieves a file from AzureBlob storage into the memory of the Azure Function.

How to Retrieve a File with an Azure Function

Creating a New Project

Let's start by creating a new project in Visual Studio. This tiputilizes screenshots taken from VS 2022.

How to Retrieve a File from Azure Blob Storage with an Azure Function (1)

Choose the Azure Function project template. In Visual Studio, you can only usethe C# programming language. You can use Visual Studio Code or program directlyin the Azure Portal if you want another language, such as PowerShell.

How to Retrieve a File from Azure Blob Storage with an Azure Function (2)

Give the project a name and choose a location to store it. If you want to addmore Azure Functions later, you might want to give the solution a different name.

How to Retrieve a File from Azure Blob Storage with an Azure Function (3)

In the next screen, you can leave the defaults. Make sure the trigger is HTTP.

How to Retrieve a File from Azure Blob Storage with an Azure Function (4)

Note -You could also use a blob trigger. In this case, theAzure Function will be executed every time a new file is created or changed. Theblob itself will be passed along to the Function, so there's not much effortrequired to read it. However, in this solution, we want more control over when andhow the Function is triggered, hence the choice for the HTTP trigger. If you wantmore info about blob triggers, check out the tipProcess Blob Files Automatically using an Azure Function with Blob Trigger.

A new Azure Function is created. It contains a bunch of template code:

How to Retrieve a File from Azure Blob Storage with an Azure Function (5)

In short, it does the following:

  1. The function is triggered by an HTTP trigger. The body and other informationare stored in the HttpRequest object named req.
  2. The function logs that it is started the processing.
  3. It assumes a property called name is passed along in thebody of the HttpRequest. It tries to read this directly (line 22), then parsesthe body (which is in JSON format) and tries to retrieve name again (lines 24-26).
  4. Both results from the previous steps are coalesced together in line 26.
  5. A response is created containing the name that was parsed in the previoussteps. The response is returned to the client in line 32. If no name was found,a message is displayed saying a name should be passed.

First, we will add a connection string to the Azure Blob Storage account. Inthe project, there's a file called local.settings.json. Inthere, we can find a key with the name AzureWebJobsStorage. Fill in thename of the Azure Blob Storage account and the account key (which can be found inthe Azure Portal).

How to Retrieve a File from Azure Blob Storage with an Azure Function (6)

If the storage account you want to use differs from the storage account usedby the Azure Function, you can add a new key-value pair to the JSON file.

By adding a few lines of code, we can read this config file into our Azure Function:

How to Retrieve a File from Azure Blob Storage with an Azure Function (7)

This ConfigurationBuilder object actually does a lot more. It will firstcheck if configuration values can be found in the Azure Function App (which willhost the Azure Function when deployed in the cloud). This means we can specify configurationparameters for when the Azure Function is running in its serverless environmentin Azure. In the next step (line 32), the contents of the local.settings.json fileare checked. This is needed to debug our Azure Function locally in Visual Studio.This settings file is not deployed to the Azure Function App! In fact, itshould not even be in source control since it contains secrets. If bothare not found, the configuration builder checks environment variables or build parameters(lines 33-34).

(Video) Create Files in Azure Blob Storage with Azure Function

Finally, in line 36, we read the connection string for the Azure Storage accountinto a variable from the config.

Connecting to an Azure Blob Container

Before we can connect to a container, we need to install some additional packagesinto our project. This is done through the Nuget Package Manager,which comes with Visual Studio. Right-click Packages under Dependencies and choose Manage Nuget Packages...

How to Retrieve a File from Azure Blob Storage with an Azure Function (8)

In the package manager, go to Browse and search for azure.storage.blobs.Install the package. It's possible you have to accept one or more licenseagreements.

How to Retrieve a File from Azure Blob Storage with an Azure Function (9)

In the code of the Azure Function, add a reference to our new package:

How to Retrieve a File from Azure Blob Storage with an Azure Function (10)

Let's add some code that will create a connection to a specific blob container,loop over all the items in that container, and write each filename to the log. Thisis done by creating an instance of the classBlobContainerClient, which accepts two arguments for its constructor: a connectionto a storage account – which we retrieved earlier from the config –and the name of the container ("mssqltips" in this use case).

How to Retrieve a File from Azure Blob Storage with an Azure Function (11)

Then we can use the methodGetBlobsByHierarchy, which will return a list ofBlobHierarchyItems. For each item, we get the blob and log its name.

string blobconnection = config["AzureWebJobsStorage"];string container = "mssqltips"; BlobContainerClient containerClient = new BlobContainerClient(blobconnection, container);foreach(BlobHierarchyItem item in containerClient.GetBlobsByHierarchy()){ log.LogInformation(item.Blob.Name);}

When we run the Azure Function in Visual Studio (hit F5), a command line promptwill open where you can retrieve the local URL of the Azure Function:

How to Retrieve a File from Azure Blob Storage with an Azure Function (12)

The Azure Function will be triggered if you open a browser and go to this URL.When it's triggered, you can see all of the names of the different blobs inthe blob container being logged to the console:

How to Retrieve a File from Azure Blob Storage with an Azure Function (13)

Now that we've verified that we can connect successfully to an Azure Storageaccount and blob container, we can try to retrieve a single blob file.

Retrieving a Blob from a Blob Container

The rest of the code looks like this:

How to Retrieve a File from Azure Blob Storage with an Azure Function (14)

First, we define some variables holding the filename of the blob we want to downloadand the name of the folder where this blob is located. We concatenate this togetherinto a full file path.

Then we create an instance of aBlobDownloadResult. Inside a try-catch block, we try to download the file. Nowwe're using the methodsGetBlobClient and the asynchronousDownloadContentAsync to retrieve the blob from the Azure service. The resultof the method DowloadContentAsync is stored in the BlobDownloadResultinstance.

When we debug our Azure Function again (I've put a breakpoint on the lastline), we can see that the function has retrieved the file successfully. However,we cannot inspect its contents directly since it's a binary file.

(Video) How to setup a Azure Blob storage trigger using Azure Functions | Azure Functions | Microsoft Azure

How to Retrieve a File from Azure Blob Storage with an Azure Function (15)

In the next tip, we'll look at how we can read the contents of an Excelfile inside an Azure Function.

Next Steps
  • As an exercise for the reader, try to pass the file name and the folderas parameters through the HTTP request body instead of hardcoding them.
  • You can find more Azure tips inthis overview. You can filter on "function" to find all thetips related to Azure Functions.
  • You can download the sample codehere.

Related Articles

How to Schedule T-SQL Statements to Run with Azure Functions

Create an Azure Function to Connect to a Snowflake Database - Part 1

Create an Azure Function to execute SQL on a Snowflake Database - Part 2

Process Blob Files Automatically using an Azure Function with Blob Trigger

Schedule Azure SQL DB Processes using Azure Functions

Popular Articles

Date and Time Conversions Using SQL Server

Format SQL Server Dates with FORMAT Function

(Video) 19. Blob Storage Bindings for Azure Functions Overview

SQL Server CROSS APPLY and OUTER APPLY

SQL NOT IN Operator

SQL Server DROP TABLE IF EXISTS Examples

SQL Server Cursor Example

How to tell what SQL Server versions you are running

Rolling up multiple rows into a single row and column for SQL Server data

SQL Convert Date to YYYYMMDD

Resolving could not open a connection to SQL Server errors

SQL Server Loop through Table Rows without Cursor

Add and Subtract Dates using DATEADD in SQL Server

Concatenate SQL Server Columns into a String with CONCAT()

(Video) Upload & Download Azure Blob File from Azure Function - 2022

SQL Server Database Stuck in Restoring State

Format numbers in SQL Server

Using MERGE in SQL Server to insert, update and delete at the same time

SQL Server Row Count for all Tables in a Database

How to Get Current Date in SQL Server

Understanding the SQL Server NOLOCK hint

Ways to compare and find differences for SQL Server tables and data

About the author

Koen Verbeeck is a seasoned business intelligence consultant at AE. He has over a decade of experience with the Microsoft Data Platform in numerous industries. He holds several certifications and is a prolific writer. He has spoken at PASS, SQLBits, dataMinds Connect and delivers webinars on MSSQLTips.com. Koen has been awarded the Microsoft MVP data platform award for many years.

View all my tips

Article Last Updated: 2023-01-30

FAQs

How do I access blob storage from Azure function? ›

The connection to this account is already stored in an app setting named AzureWebJobsStorage .
  1. Configure your local environment. ...
  2. Download the function app settings. ...
  3. Register binding extensions. ...
  4. Add an output binding. ...
  5. Add code that uses the output binding. ...
  6. Redeploy and verify the updated app. ...
  7. Clean up resources. ...
  8. Next steps.
Jan 31, 2023

How do I extract data from Azure blob storage? ›

In this tutorial, you learn how to:
  1. Prerequisites to export data from Azure Blob storage with Azure Import/Export.
  2. Step 1: Create an export job.
  3. Step 2: Ship the drives.
  4. Step 3: Update the job with tracking information.
  5. Step 4: Receive the disks.
  6. Step 5: Unlock the disks.
Jan 13, 2023

Can we query data from Azure blob storage? ›

The Query Blob Contents operation applies a simple Structured Query Language (SQL) statement on a blob's contents and returns only the queried subset of the data. You can also call Query Blob Contents to query the contents of a version or snapshot.

How do I copy a file from Azure blob storage using PowerShell? ›

If you need to install or upgrade, see Install Azure PowerShell module.
  1. Sign in to Azure. ...
  2. Create a resource group. ...
  3. Create a storage account. ...
  4. Create a container. ...
  5. Upload blobs to the container. ...
  6. Download blobs. ...
  7. Data transfer with AzCopy. ...
  8. Clean up resources.
Nov 7, 2022

How do I access my Azure storage files? ›

Mount the Azure file share
  1. Sign in to the Azure portal.
  2. Navigate to the storage account that contains the file share you'd like to mount.
  3. Select File shares.
  4. Select the file share you'd like to mount.
  5. Select Connect.
  6. Select the drive letter to mount the share to.
  7. Copy the provided script.

How do you use the blob trigger in Azure functions? ›

Create an Azure Blob storage triggered function
  1. Select Functions, and then select + Add to add a new function.
  2. Choose the Azure Blob Storage trigger template.
  3. Use the settings as specified in the table below the image. Setting. Suggested value. Description. New Function. ...
  4. Select Create Function to create your function.
May 11, 2020

Videos

1. Upload & Download Azure Blob File from Azure Function - 2022
(AkashBhat)
2. Upload files to Microsoft Azure Blob storage with C# | Azure Storage Account | .Net Core
(AzureTeach•Net)
3. 16. Connect Functions to Azure Storage
(WafaStudies)
4. Azure Blob Storage -Access Blob From Azure Storage Account Using Postman Tool
(AzureTeach•Net)
5. What is the Azure Blob Storage? | How to Use the Azure Blob Storage
(ITProTV)
6. EXECUTE AZURE FUNCTION WITH BLOB TRIGGER
(asar cloud Chef)
Top Articles
Latest Posts
Article information

Author: Domingo Moore

Last Updated: 30/05/2023

Views: 6601

Rating: 4.2 / 5 (73 voted)

Reviews: 88% of readers found this page helpful

Author information

Name: Domingo Moore

Birthday: 1997-05-20

Address: 6485 Kohler Route, Antonioton, VT 77375-0299

Phone: +3213869077934

Job: Sales Analyst

Hobby: Kayaking, Roller skating, Cabaret, Rugby, Homebrewing, Creative writing, amateur radio

Introduction: My name is Domingo Moore, I am a attractive, gorgeous, funny, jolly, spotless, nice, fantastic person who loves writing and wants to share my knowledge and understanding with you.