If you want to keep track of changes in a Blob container, or notify someone when there occurs a change in a Blob container, it is possible to use a Logic app that is triggered when a change occurs.
This blog contains a simple demo that shows you how to use a change in a Blob container as a trigger for a Logic App. The Logic App will use polling on the Blob container to check if a change occurs.
Triggering the Logic App
In this hands-on demo we’ll be creating a Logic App that polls a Blob container. If any change occurred between the last polling and this polling, the Logic App is fired.
Creating a storage account
The first step is to create a storage account in Azure. This storage account will contain the blob container and the Azure table that we are going to use in our Logic App. In the Azure Portal, select “New” and search for “Storage Account”.
Fill in the required fields. In the Account kind make sure you select “General purpose” to be able to use the storage account for both Blobs and table storage. I chose a Standard performance and “LRS” Replication because in this demo the performance and replication type doesn’t matter for us.
Once the creation is complete, open the created storage account from the available resources.
In the storage account we are going to add a blob container and a table.
First select the “Blobs” under the services and add a new container. Choose a name for the container and leave the access level at private. For this demo it is sufficient that we (as owner of the storage account) are able to access this container.
Next we will create a table table to the storage account. Go back to the storage account overview and select the table service.
Choose a name for the table and click “OK”.
Designing the Logic App
Now that we have created our resources that we need for our Logic App, we are ready to create the Logic App. To create the Logic App select “New” in the Azure Portal and search for “Logic App”.
Fill in the required fields, and click the “Create”-button.
We will build this Logic App from scratch, so in the templates you can select “Blank Logic App”.
The first element we need is the trigger. Search for Blob and then select the Azure Blob Storage trigger. Notice that this trigger is still under preview as this blog is written, so it is possible that the component is changed when you are reading this blog post.
We will be prompted to create a new storage account connection. Choose a name for the connection, and select the storage account you created earlier from the list:
Now that we created the storage account connection, we are able to select the blob container we created earlier. For this demo a polling interval of 5 minutes is sufficient.
The next step is to write the data we receive from the blob container polling to an Azure table. Create a new component and search for “Azure Table”. Select the “Azure Table Storage – Insert or Replace Entity” action. Notice that this action is also still in Preview.
We have to create another Storage Account Connection for the table service.
When this is done, we have to select the table we want to write an entity to. We will select the table we created earlier. In the Partition key, we will fill in a static value. In the row key we will select the Id from the “Blog Metadata” retrieved in the trigger. The combination of Partition- and Row-Key should be unique. In the Entity field we have to create a JSON object containing the values we want to add to the entity. This example uses the DisplayName of the file that was created or updated and the “LastModified” timestamp. So the table will contain a log of every update in the Blob container.
The complete logic app should look like like this. This is a very simple flow with only one action. Make sure to save the Logic App.
Testing the Logic App
The Logic App will trigger on any change done in the Blob container. To test this trigger, we will create a file in the Blob container.
Go back to all resources and open the Storage Account.
In the Storage Account services we will go to Blobs, and select the right container.
In the blob container we will select “Upload” and then browse the file we want to upload.
If we go back to the Logic App we can check if the Logic App was triggered. Normally you should see a successful run in the Runs history. If you don’t see any run, you can click the Run trigger to force the polling to run.
To validate the input for the Insert you can click the “Show raw inputs” link.
The JSON code below shows the request to the Insert or Replace Entity action. It contains the Partition- and Row-key in the path. The other values are added in the “Body” object.
To visualize the Azure table, we will use the Azure Storage Explorer. If you don’t have this application installed yet you can download it from https://azure.microsoft.com/en-us/features/storage-explorer/ . Launch the application and connect to your Azure Subscription. Browse the table in the storage account and you will see the table contains a row with the information we selected earlier in the demo.
There are many possibilities when working with Azure Logic Apps. Triggering a Logic App on a change in a Blob Container can be useful in business case. The demo in this blog shows an easy example to get started with this functionality.
Author: Jasper Meeusen
Integration consultant at Integration.team