How to Create Blob Trigger Azure Function In Visual Studio

How to Create Blob Trigger Azure Function In Visual Studio

In this Azure article, we will discuss how to create a blob trigger Azure Function in visual studio 2019. Along with that, we will also see how to deploy that in Azure Portal.

How to Create Blob Trigger Azure Function In Visual Studio

Before starting the actual development, let’s discuss the prerequisites needed here.

Prerequisites 

  • We need an Azure Account or Azure Subscription. Don’t have an Azure Account, no need to worry, create an Azure Free Account now.
  • If you don’t have Visual Studio 2019 installed on your dev machine, install Visual Studio 2019 now. Make sure to install the Azure development workload Along with Visual Studio 2019.
  • Make sure you have the latest version of Azure Function tools.

Assuming you are ready with all the Prerequisites needed here. Let’s start the creation of the Azure Function Project.

Open the Visual Studio 2019 and On the Create a new project window, click on the Create a New Project button. You need to select the Azure Functions template and then click on the Next button.

Create Blob Trigger Azure Function In Visual Studio

Provide the Project name, and choose a location for your project where you want to store your project from the below window and then click on the Create button.

how to create azure function blob trigger

Select the trigger template as Blob trigger and Storage Account (AzureWebJobsStorage) as Storage Emulator and then the next is Provide a name for the connection string setting and then click on the Create button

How to Create Blob Trigger Azure Function In Visual Studio 2019

Now as you will see below, the project was created successfully without any issue

blob trigger azure function

Below is the code for the Azure Functions code that got created

public static class Function1
    {
        [FunctionName("Function1")]
        public static void Run([BlobTrigger("samples-workitems/{name}", Connection = "MyDemoConnection")]Stream myBlob, string name, ILogger log)
        {
            log.LogInformation($"C# Blob trigger function Processed blob\n Name:{name} \n Size: {myBlob.Length} Bytes");
        }
    }

The next step is we should make sure that the connection string is there with the same name that we provide before the step i.e “MyDemoConnection” with the key-value pair in the local.settings.json file. Open the local.settings.json file.

So the key here is “MyDemoConnection” and we need the value for this, to get this value we need to log in to the Azure Portal. Log in to the Azure Portal, then navigate to the storage account and then click on the “Access keys” under settings from the left navigation. As highlighted below, You need to copy the connection string.

blob trigger azure function c#

Now open your local.settings.json file and do the below changes, where you need to add the key and value pair for your Connection String. The local.settings.json file should look like the one below now.

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "UseDevelopmentStorage=true",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",
    "MyDemoConnection": "DefaultEndpointsProtocol=https;AccountName=mynewstoragedemo;AccountKey=pNWgITmf33ROgS1ixyUAQ4fIQIhaqBUWQjwI79/PjLy9zRoqEsvOQ7fn1gu41sj+dQ9eCHA/sDew8/OhDMfI9w==;EndpointSuffix=core.windows.net"
  }
}
blob trigger azure function example

Now your function to work, it needs someplace to write the information. So you can write the information into the “AzureWebJobsStorage”.

{
    "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "DefaultEndpointsProtocol=https;AccountName=mynewstoragedemo;AccountKey=pNWgITmf33ROgS1ixyUAQ4fIQIhaqBUWQjwI79/PjLy9zRoqEsvOQ7fn1gu41sj+dQ9eCHA/sDew8/OhDMfI9w==;EndpointSuffix=core.windows.net",
    "FUNCTIONS_WORKER_RUNTIME": "dotnet",

    "MyDemoConnection": "DefaultEndpointsProtocol=https;AccountName=mynewstoragedemo;AccountKey=pNWgITmf33ROgS1ixyUAQ4fIQIhaqBUWQjwI79/PjLy9zRoqEsvOQ7fn1gu41sj+dQ9eCHA/sDew8/OhDMfI9w==;EndpointSuffix=core.windows.net"
  }
}

You can see like below

blob trigger azure function c# example

To upload a file, to the storage container, you can do that using the Azure Storage Explorer

Open the Azure Storage Explorer, and click on the Connect to Azure Storage button from the left navigation as highlighted below. select the Use a connection string option and then click on the Next button.

create blob trigger azure function in visual studio 2019

You can paste the same connection string as above. The display name will populate automatically, once you will put the value of the connection string and then click on the Next button.

create blob trigger azure function

Now it will show you the connection Summary details with all the information below. Now click on the Connect button

how to create a blob trigger function azure

You can able to see all the connection details and storage account details, Expand the

How to Develop a Blob Trigger Azure Function In Visual Studio 2017

Provide the container name as samples-work-items which we have mentioned above as the path value. Click on the Upload and upload a file in your container. Then again you can run the Azure Function App again.

create blob trigger azure function in visual studio 2019

I have already uploaded one file in the samples-workitems container. Now if you will run (F5) the application, you can able to see the expected output

How to Develop Blob Trigger Azure Function In Visual Studio 2019

Deploy Blob Trigger Azure Function From Visual Studio 2019

Now To deploy the Blob Trigger Azure Function From Visual Studio 2019, You need to follow the below steps

Right-click on the Project name and click on the Publish option.

Deploy Blob Trigger Azure Function From Visual Studio 2019

Choose the Azure Functions Consumption Plan and then select the Create New option and then click on the Create Profile button.

How to Deploy Blob Trigger Azure Function From Visual Studio 2019

On the App Service window, Provide the below details

  • Name: You need to provide the name for the Function App
  • SubscriptionChoose your correct Azure subscription
  • Resource group: You can click on the New button to create a new Resource Group or You can choose your existing resource group.
  • Location: Choose your Function App location
  • Azure Storage: If you don’t have an existing storage account then click on the New button to create a new Storage account else You can choose your existing storage account.

Now click on the Create button.

How to Deploy Blob Trigger Azure Function From Visual Studio 2017

Now you can able to see the Azure Function App created successfully without any issues. Now click on the Publish button.

How to Deploy Blob Trigger Azure Function From Visual Studio

Now, log in to the Azure Portal and navigate to the Azure Function App that you have created from the Visual Studio above, and on the Function App window, click on the Functions option from the left navigation. Then you will see the Azure Functions that we have created from the visual studio. Click on the Function and on the Function page, click on the Code + Test link, and then click on the Test/Run button.

How to publish Blob Trigger Azure Function From Visual Studio

Click on the Run button from the below window.

How to publish Blob Trigger Azure Function From Visual Studio 2019

You can able to see We got the correct response code i.e. 202 Accepted.

How to publish Blob Trigger Azure Function From Visual Studio 2017

You may also like following the below articles

Wrapping Up

In this Azure article, we discussed, how to create and deploy a blob trigger Azure Function in visual studio 2019. Thanks for reading this article !!!