Create a function that’s triggered by Blob storage
This tutorial will help you in Creating a function that’s triggered by Blob storage. You will Learn how to create a function triggered when files are uploaded to or updated in a Blob storage container.
Creating a function that’s triggered by Blob storage
- Log in to the Azure portal (https://portal.azure.com).
- Click “Create a resource” in the left-hand navigation bar and select “Function App”.
- In the “Function App” blade, provide a unique name for your Function App and choose a subscription, resource group, and hosting plan. You can also configure the storage account settings and choose a runtime stack.
- Click “Create” to create the Function App.
- Once the Function App is created, click on the “Functions” blade and select “New Function”.
- Choose “Azure Blob Storage trigger” as the function template and provide a name for your function. You can also choose to create a new storage account or use an existing one.
- In the “Azure Blob Storage trigger” configuration window, specify the Blob storage container you want to monitor and select the trigger type. You can choose to trigger the function when a blob is added, modified or deleted. You can also specify a filter to only trigger the function for specific file types or file names.
- Click “Create” to create the function.
- In the function editor window, write the code that will be executed when the function is triggered by a Blob storage event. You can use the input binding to read the contents of the Blob and perform any necessary processing, such as data transformation, parsing or validation.
- Save your function code and click “Run” to test it with a sample Blob. You can also use the “Test/Run” tab to specify input and output bindings and test your function with real-world data.
- Once you’re satisfied with your function, you can deploy it to your Function App by clicking “Deploy” in the upper-right corner of the editor window. You can also configure the function settings, such as environment variables, application settings, and access controls.
- To monitor your function and view its logs, go to the “Monitor” tab and select “Logs”. You can use the log data to diagnose issues and optimize the performance of your function.
Creating an Azure Function app
Firstly, Go to Azure portal menu or the Home page, choose Create a resource.
Now, In the New page, choose Compute > Function App.
Also, On the Basics page, you use the function app settings as specified in the following table.
Setting | Suggested value | Description |
---|---|---|
Subscription | Your subscription | The subscription under which this new function app is created. |
Resource Group | myResourceGroup | Name for the new resource group in which to create your function app. |
Function App name | Globally unique name | Name that identifies your new function app. Valid characters are a-z (case insensitive), 0-9 , and - . |
Publish | Code | Option to publish code files or a Docker container. |
Runtime stack | Preferred language | Choose a runtime that supports your favorite function programming language. Choose .NET Core for C# and F# functions. |
Version | Version number | Choose the version of your installed runtime. |
Region | Preferred region | Choose a region near you or near other services your functions access. |
Further steps
choose Next : Hosting. Fill in the following information on the Hosting page.
Setting | Suggested value | Description |
---|---|---|
Storage account | Globally unique name | Create a storage account used by your function app. Storage account names must be between 3 and 24 characters in length and can contain numbers and lowercase letters only. You can also use an existing account, which must meet the storage account requirements. |
Operating system | Preferred operating system | An operating system is pre-selected for you based on your runtime stack selection, but you can change the setting if necessary. |
Plan | Consumption (Serverless) | Hosting plan that defines how resources are allocated to your function app. In the default Consumption plan, resources are added dynamically as required by your functions. In this serverless hosting, you pay only for the time your functions run. When you run in an App Service plan, you must manage the scaling of your function app. |
choose Next : Monitoring. On the Monitoring page, enter the following settings.
Setting | Suggested value | Description |
---|---|---|
Application Insights | Default | Creates an Application Insights resource of the same App name in the nearest supported region. By expanding this setting or selecting Create new, you can change the Application Insights name or choose a different region in an Azure geography where you want to store your data. |
On the Review + create page, go through your settings, and then choose Create to provision and deploy the function app.
choose the Notifications icon in the upper-right corner of the portal and watch for the Deployment succeeded message.
To see your new function app, select Go to the resource. You may also choose to pin it to your dashboard. It’s easy to return to this function app resource from your dashboard if you pin it.
You’ve successfully created your new function app.
Create the container
- In your function, on the Overview page, choose your resource group.
Find and choose your resource group’s storage account.
Select Containers, and then select + Container.
In the Name field, type samples-workitems
, and then choose Create.
Now that you have a blob container, you can test the function by uploading a file to the container.
Create an Azure Blob storage triggered function
- choose Functions, and then choose + Add to add a new function.
- Select the Azure Blob Storage trigger template.
- Use the settings as mentioned in the table below
Setting | Suggested value | Description |
---|---|---|
New Function | Unique in your function app | Name of this blob triggered function. |
Path | samples-workitems/{name} | Location in Blob storage being monitored. The file name of the blob is passed in the binding as the name parameter. |
Storage account connection | AzureWebJobsStorage | You can use the storage account connection already being used by your function app, or create a new one. |
choose Create Function to create your function.
Next, choose the samples-workitems container.
Test the function
- Firstly, get Back in the Azure portal, browse to your function expand the Logs at the bottom of the page and make sure that log streaming is working.
- Then In a separate browser window, go to your resource group in the Azure portal, and choose the storage account.
- Also,choose Containers, and then choose the samples-workitems container.
- choose Upload, and then select the folder icon to select a file to upload.
- Subsequently, Browse to a file on your local computer, such as an image file, choose the file. choose Open and then Upload.
- Lastly, Go back to your function logs and make sure that the blob has been read.
Last and important step is to clean up the resources when you are done.
AZ-305 Exam Practice Questions
Question 1: ABC Inc. wants to create an Azure Function that gets triggered whenever a new blob is added to a specific container in their Blob storage account. Which type of trigger should they use?
A. Blob trigger
B. HTTP trigger
C. Queue trigger
D. Timer trigger
Answer: A. Blob trigger
Explanation: A Blob trigger is used to run an Azure Function whenever a new blob is added to a specified container in Blob storage. In this case, ABC Inc. wants to trigger the function whenever a new blob is added to a specific container, so they should use a Blob trigger.
Question 2: XYZ Corp. wants to create an Azure Function that gets triggered whenever a blob is deleted from a specific container in their Blob storage account. Which type of trigger should they use?
A. Blob trigger
B. HTTP trigger
C. Queue trigger
D. Timer trigger
Answer: None of the above.
Explanation: Currently, there is no built-in trigger for an Azure Function to be triggered when a blob is deleted from a specific container. However, you can use an Event Grid trigger to trigger an Azure Function when an event, such as blob deletion, occurs. In this case, XYZ Corp. should use an Event Grid trigger.
Question 3: PQR Enterprises wants to create an Azure Function that processes only specific types of blobs in their Blob storage account. Which attribute of the Blob trigger should they use to accomplish this?
A. path
B. name
C. metadata
D. direction
Answer: B. name
Explanation: The name attribute of the Blob trigger can be used to process only specific types of blobs in a Blob storage account. By specifying a pattern for the name attribute, you can ensure that the function is triggered only when a blob with a matching name is added to the specified container.
Question 4: DEF Corp. wants to create an Azure Function that processes multiple blobs at the same time whenever a new blob is added to a specific container in their Blob storage account. Which property of the Blob trigger should they use to accomplish this?
A. SingleBlob
B. MultipleBlobs
C. Batch
D. Sequential
Answer: B. MultipleBlobs
Explanation: The MultipleBlobs property of the Blob trigger can be used to process multiple blobs at the same time when a new blob is added to a specified container. By setting this property to true, the function can handle multiple blobs in a single invocation, which can improve the efficiency of the function.
Reference documentation – Create a function in Azure that’s triggered by Blob storage