Send Email with multiple attachment using Azure Logic App - azure

I need to send the blobs uploaded to my Azure storage container as an attachments. Number of files getting uploaded to container will change so I need to use dynamic method for attachment.
I have verified this question related to it
I am using below logic:
Append to variable values
{
"Name": items('For_each')?['DisplayName']
"ContentBytes":body('Get_blob_content')
}
When I am trying to save the logic, getting below error:
Save logic app failed
Failed to save logic app testing. The template validation failed: 'The action(s) 'Get_blob_content' referenced by 'inputs' in action 'Append_to_array_variable' are not defined in the template.'.
How can I solve this ?

Based on the error message shared above, Instead of saving the entire workflow at once would suggest you to save the logic app at each stage or before the appendtoarray variable stage & post then append the values to attachment variable with previous stage outputs.
Based on the above requirement, we have created the below logic app in our local environment &tested it as well which is working fine.
In our workflow, We have Used For Each to loop the blobs from List Blobs action. Within For Each you can use Get blob content to get blob content, and then use Append to array variable to append attachments.
The expressions Name and ContentBytes are as follows:
"ContentBytes": "#base64(body('Get_blob_content_(V2)'))",
"Name": "#items('For_each')?['DisplayName']"
Here is the code view of the Logic app that we have created :
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"Append_to_array_variable": {
"inputs": {
"name": "attachments",
"value": {
"ContentBytes": "#base64(body('Get_blob_content_(V2)'))",
"Name": "#items('For_each')?['DisplayName']"
}
},
"runAfter": {
"Get_blob_content_(V2)": [
"Succeeded"
]
},
"type": "AppendToArrayVariable"
},
"Get_blob_content_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/#{encodeURIComponent(encodeURIComponent(items('For_each')?['Path']))}/content",
"queries": {
"inferContentType": true
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"foreach": "#body('Lists_blobs_(V2)')?['value']",
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "attachments",
"type": "array"
}
]
},
"runAfter": {
"Lists_blobs_(V2)": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Lists_blobs_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2/#{encodeURIComponent(encodeURIComponent('JTJmcmVwb3J0cw=='))}",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"metadata": {
"JTJmcmVwb3J0cw==": "/reports"
},
"runAfter": {},
"type": "ApiConnection"
},
"Send_an_email_(V2)": {
"inputs": {
"body": {
"Attachments": "#variables('attachments')",
"Body": "<p>tested logic app flow successfully</p>",
"Subject": "blob test",
"To": "<username>#microsoft.com"
},
"host": {
"connection": {
"name": "#parameters('$connections')['office365']['connectionId']"
}
},
"method": "post",
"path": "/v2/Mail"
},
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"When_a_blob_is_added_or_modified_(properties_only)_(V2)": {
"evaluatedRecurrence": {
"frequency": "Minute",
"interval": 1
},
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/triggers/batch/onupdatedfile",
"queries": {
"checkBothCreatedAndModifiedDateTime": false,
"folderId": "JTJmcmVwb3J0cw==",
"maxFileCount": 10
}
},
"metadata": {
"JTJmcmVwb3J0cw==": "/reports"
},
"recurrence": {
"frequency": "Minute",
"interval": 1
},
"splitOn": "#triggerBody()",
"type": "ApiConnection"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<sub-ID>/resourceGroups/<resourceGroup>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<sub-id>/providers/Microsoft.Web/locations/eastus/managedApis/azureblob"
},
"office365": {
"connectionId": "/subscriptions/<sub-id>/resourceGroups/<resroucegroup>/providers/Microsoft.Web/connections/office365",
"connectionName": "office365",
"id": "/subscriptions/<sub-id>/providers/Microsoft.Web/locations/eastus/managedApis/office365"
}
}
}
}
}
Here is the sample Output for reference:

Related

How do you populate an Azure IP-Group using Logic Apps?

What I am trying to accomplish:
I am trying to do a HTTP GET request from Azure Logic Apps from https://endpoints.office.com/endpoints/Worldwide?ClientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7 where they provide public facing list of IP addresses for Office365.
I would like to only pull out the IPv4 list of addresses into a legible format and then store this into my Blob storage in Azure as a CSV file.
Then what I want is that the list of IPv4 addresses to be pulled into an IP-GROUP in Azure so that any changes of IP addresses from Microsoft would be dynamically updated via Logic Apps and stored into Blob Storage and then updated in the IP-Group.
The IP-Group is used in an Azure Firewall.
What I have Tried [logic app in code]:
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Create_blob_(V2)": {
"inputs": {
"body": "#triggerBody()",
"headers": {
"ReadFileMetadataFromServer": true
},
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "post",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files",
"queries": {
"folderPath": "/project-a",
"name": "office365-test.csv",
"queryParametersSingleEncoded": true
}
},
"runAfter": {
"Create_job": [
"Succeeded"
]
},
"runtimeConfiguration": {
"contentTransfer": {
"transferMode": "Chunked"
}
},
"type": "ApiConnection"
},
"Create_job": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureautomation']['connectionId']"
}
},
"method": "put",
"path": "/subscriptions/#{encodeURIComponent('xxxxxxxxxxxxxxxxxxxxxxxxx')}/resourceGroups/#{encodeURIComponent('xxxxxxx')}/providers/Microsoft.Automation/automationAccounts/#{encodeURIComponent('test')}/jobs",
"queries": {
"runbookName": "test-hello",
"wait": false,
"x-ms-api-version": "2015-10-31"
}
},
"runAfter": {},
"type": "ApiConnection"
},
"Get_blob_content_using_path_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/GetFileContentByPath",
"queries": {
"inferContentType": true,
"path": "/project-a/office365-test.csv",
"queryParametersSingleEncoded": true
}
},
"runAfter": {
"Create_blob_(V2)": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"HTTP": {
"evaluatedRecurrence": {
"frequency": "Day",
"interval": 1
},
"inputs": {
"method": "GET",
"uri": "https://endpoints.office.com/endpoints/Worldwide?ClientRequestId=b10c5ed1-bad1-445f-b386-b919946339a7"
},
"recurrence": {
"frequency": "Day",
"interval": 1
},
"type": "Http"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureautomation": {
"connectionId": "/subscriptions/xxxxxx/resourceGroups/xxx/providers/Microsoft.Web/connections/azureautomation",
"connectionName": "azureautomation",
"id": "/subscriptions/xxxxxx/providers/Microsoft.Web/locations/uksouth/managedApis/azureautomation"
},
"azureblob": {
"connectionId": "/subscriptions/xxxxxx/resourceGroups/xxx/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/xxxxxx/providers/Microsoft.Web/locations/uksouth/managedApis/azureblob"
}
}
}
}
}
This pulls the whole lot of data from Microsoft Website and bundles it into blob storage in a messy format. I only need the IPv4 addresses in a clear format for CSV.
Then I would like that CSV to be used to update an existing IP-GROUP. Any suggestions?

Listing and viewing contents of Azure Storage containers with Azure Logic Apps?

In Azure a container will be made for each user in Azure Storage, I want to be able to list the container and view the size of the content to ensure it does not exceed a certain limit. Is it possible to do this with Azure Logic Apps? I am not finding functions to do so.
yes, this is possible through logic apps. You can use 'Get Blob Metadata (V2)' to get all the properties of the files and if you are trying to get the content, you can use 'Get blob content (V2)'. To reproduce this I first created 3 containers (i.e., Container1, Container2, and Container3) and uploaded a few files into these containers with different sizes. To get the list of all the containers I have used Lists blobs in the root folder (V2) and used Lists blobs (V2) taking the path from the previous step to iterate blobs inside the containers.
In the next step I'm using Get Blob Metadata (V2) to get the properties of each blob using list blobs path.
Then using Condition action I'm extracting the blobs which are less than 9128 bytes and storing all this data into variables.
Finally, I'm using compose connector to view all the files along with size and path.
RESULT:
You can try using the below code view to test in your environment
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": "#variables('FilesWithinLimit')",
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "Compose"
},
"Compose_2": {
"inputs": "#variables('FilesOutOfLimit')",
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "Compose"
},
"FilesOutOfLimit": {
"inputs": {
"variables": [
{
"name": "FilesOutOfLimit",
"type": "array"
}
]
},
"runAfter": {
"FilesWithinLimit": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"FilesWithinLimit": {
"inputs": {
"variables": [
{
"name": "FilesWithinLimit",
"type": "array"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"For_each": {
"actions": {
"For_each_2": {
"actions": {
"Condition": {
"actions": {
"Append_to_FilesWithinLimit": {
"inputs": {
"name": "FilesWithinLimit",
"value": {
"Name": "#{body('Get_Blob_Metadata_(V2)')?['DisplayName']}",
"Path": "#{body('Get_Blob_Metadata_(V2)')?['Path']}",
"Size": "#{body('Get_Blob_Metadata_(V2)')?['Size']}"
}
},
"runAfter": {},
"type": "AppendToArrayVariable"
}
},
"else": {
"actions": {
"Append_to_FilesOutOfLimit": {
"inputs": {
"name": "FilesOutOfLimit",
"value": {
"Name": "#{body('Get_Blob_Metadata_(V2)')?['DisplayName']}",
"Path": "#{body('Get_Blob_Metadata_(V2)')?['Path']}",
"Size": "#{body('Get_Blob_Metadata_(V2)')?['Size']}"
}
},
"runAfter": {},
"type": "AppendToArrayVariable"
}
}
},
"expression": {
"and": [
{
"lessOrEquals": [
"#body('Get_Blob_Metadata_(V2)')?['Size']",
9128
]
}
]
},
"runAfter": {
"Get_Blob_Metadata_(V2)": [
"Succeeded"
]
},
"type": "If"
},
"Get_Blob_Metadata_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/#{encodeURIComponent(encodeURIComponent(items('For_each_2')?['Path']))}"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"foreach": "#body('Lists_blobs_(V2)')?['value']",
"runAfter": {
"Lists_blobs_(V2)": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_blobs_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2/#{encodeURIComponent(encodeURIComponent(items('For_each')?['Path']))}",
"queries": {
"nextPageMarker": ""
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"foreach": "#body('Lists_blobs_in_the_root_folder__(V2)')?['value']",
"runAfter": {
"Lists_blobs_in_the_root_folder__(V2)": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_blobs_in_the_root_folder__(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"runAfter": {
"FilesOutOfLimit": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<Your_SUBSCRIPTION_ID>/resourceGroups/<YOUR_RESOURCE_GROUP>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<Your_SUBSCRIPTION_ID>/providers/Microsoft.Web/locations/centralus/managedApis/azureblob"
}
}
}
}
}

How do I parse Json file from azure blob storage to Azure Sql using Azure logic app

[][1]I have a multiple json files dropping in blob storage weekly, I want to use azure logic app to parse json file and copy data into Azure Sql? Please help
For achieving your requirement, Below are the flow that you can follow :-
Blob trigger (When a blob is added or modified (properties only) (V2)) >> Get blob content using path (V2) >> Parse JSON >> SQL related Action (For instance I'm using Insert row (V2)).
Below is the sample JSON that I'm uploading to my container.
{
"employees": {
"emp_name": "abc",
"hire_date": "2022-10-23",
"salary": 10000
}
}
I'm using triggers path to get the content of the blob. While Inserting the row I'm using Parse JSON values. Below is my Logic App.
Result:
UPDATED ANSWER
As per your requirement, you can either manually trigger flow using `` or set a recurrence trigger to make the flow triggered at times and then use list all files in that particular container from the storage account. Here is how the flow looks like
detailed flow
RESULTS:
code view of my logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"For_each": {
"actions": {
"Get_blob_content_using_path_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/GetFileContentByPath",
"queries": {
"inferContentType": true,
"path": "#items('For_each')?['Path']",
"queryParametersSingleEncoded": true
}
},
"runAfter": {},
"type": "ApiConnection"
},
"Insert_row_(V2)": {
"inputs": {
"body": {
"emp_id": "#body('Parse_JSON')?['employees']?['employee_id']",
"emp_name": "#body('Parse_JSON')?['employees']?['emp_name']",
"hire_date": "#body('Parse_JSON')?['employees']?['hire_date']",
"salary": "#body('Parse_JSON')?['employees']?['salary']"
},
"host": {
"connection": {
"name": "#parameters('$connections')['sql']['connectionId']"
}
},
"method": "post",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('default'))},#{encodeURIComponent(encodeURIComponent('default'))}/tables/#{encodeURIComponent(encodeURIComponent('[dbo].[employees]'))}/items"
},
"runAfter": {
"Parse_JSON": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"Parse_JSON": {
"inputs": {
"content": "#json(body('Get_blob_content_using_path_(V2)'))",
"schema": {
"properties": {
"employees": {
"properties": {
"emp_name": {
"type": "string"
},
"employee_id": {
"type": "integer"
},
"hire_date": {
"type": "string"
},
"salary": {
"type": "integer"
}
},
"type": "object"
}
},
"type": "object"
}
},
"runAfter": {
"Get_blob_content_using_path_(V2)": [
"Succeeded"
]
},
"type": "ParseJson"
}
},
"foreach": "#body('Lists_blobs_(V2)')?['value']",
"runAfter": {
"Lists_blobs_(V2)": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_blobs_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2/#{encodeURIComponent(encodeURIComponent('JTJmY29udGFpbmVyMQ=='))}",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"metadata": {
"JTJmY29udGFpbmVyMQ==": "/container1"
},
"runAfter": {},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<SUBSCRIPTION ID>/providers/Microsoft.Web/locations/centralus/managedApis/azureblob"
},
"sql": {
"connectionId": "/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP NAME>/providers/Microsoft.Web/connections/sql",
"connectionName": "sql",
"id": "/subscriptions/<SUBSCRIPTION ID>/providers/Microsoft.Web/locations/centralus/managedApis/sql"
}
}
}
}
}

Azure Logic app how to append list blobs to array variable?

Hi I am working on azure logic app. I have below array variable. These are the mail folder path and inside I have files.
["/mycontainer/PreDesign/1e36d504-7876-41b1-89b3-83d2132fa7c4/AdditionalDocumets","/mycontainer/PreDesign/1e36d504-7876-41b1-89b3-83d2132fa7c4/TowerCalcOutPut","/mycontainer/PreDesign/1e36d504-7876-41b1-89b3-83d2132fa7c4/TowerDataSheet"]
Then I have below method to list the blobs
Here blob list is array variable as defined above. Then I have added listblobs which will loop through each path defined in array variable and lists blobs and I want to append blobs to array variable.
Here in append array variable I am able to append only blobs available inside first array element that is /containername/PreDesign/1e36d504-7876-41b1-89b3-83d2132fa7c4/AdditionalDocumets
I am not able to append blobs which are inside mycontainer/PreDesign/1e36d504-7876-41b1-89b3-83d2132fa7c4/TowerCalcOutPut","/mycontainer/PreDesign/1e36d504-7876-41b1-89b3-83d2132fa7c4/TowerDataSheet, only first element blobs I am able to append it to array variable. I am struggling to append all the blobs to array variable. Can someone help me where exactly I am missing? Any help would be appreciated. Thank you
I am struggling to append all the blobs to array variable. Can someone help me where exactly I am missing?
Its because the flow goes into only the first folder but not all folders.
Here is one of the workaround that you can try. Like said in How to attach multiple blobs in logic app from different folder just try adding Append to array variable at the end of For each 2 and you will have paths for all the blobs in your variable. Below is the screenshot of my logic app for your reference.
output
Here is the code view of my logic app
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Compose": {
"inputs": "#variables('BlobList')",
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "Compose"
},
"For_each": {
"actions": {
"For_each_2": {
"actions": {
"For_each_3": {
"actions": {
"Append_to_array_variable": {
"inputs": {
"name": "BlobList",
"value": "#items('For_each_3')?['Path']"
},
"runAfter": {},
"type": "AppendToArrayVariable"
}
},
"foreach": "#body('Lists_blobs_in_Directory')?['value']",
"runAfter": {
"Lists_blobs_in_Directory": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_blobs_in_Directory": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2/#{encodeURIComponent(encodeURIComponent(items('For_each_2')?['Path']))}",
"queries": {
"nextPageMarker": ""
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"foreach": "#body('Lists_Directories_inside_Container')?['value']",
"runAfter": {
"Lists_Directories_inside_Container": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_Directories_inside_Container": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2/#{encodeURIComponent(encodeURIComponent(items('For_each')?['Path']))}",
"queries": {
"nextPageMarker": ""
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"foreach": "#body('Lists_Containers_in_root_folder')?['value']",
"runAfter": {
"Lists_Containers_in_root_folder": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_BlobList_variable": {
"inputs": {
"variables": [
{
"name": "BlobList",
"type": "array"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Lists_Containers_in_root_folder": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"runAfter": {
"Initialize_BlobList_variable": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"manual": {
"inputs": {
"schema": {}
},
"kind": "Http",
"type": "Request"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<YOUR_SUBSCRIPTION_ID>/resourceGroups/<YOUR_RESOURCE_GROUP>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<YOUR_SUBSCRIPTION_ID>/providers/Microsoft.Web/locations/northcentralus/managedApis/azureblob"
}
}
}
}
}

How to monitor the creation of Blob in Azure Storage container/subfolders and Trigger a logic app to send email

We have Azure storage container with subfolders created dynamically, we want to monitor the creation of blob under this container/subfolders and trigger an email once per day with all the blobs added for the day as an attachment in the email.
What we tried is to create a Logic app with Event Grid Trigger (When a resource event occurs) like below.
we would like to know if there is way we can collect all the blobs added in a day under the container and get the contents of all those blobs and add it as an attachment and trigger an email at scheduled time once per day.
Any suggestion on
how to get all the blobs under a container, sub-folder and store the blobs metadata created as an array?
And then how to attach all the blobs created in a single email?
How to get the blob name after, "When a resource event occurs" trigger?
Based on the above requirements, we have created a logic app using timer to list & get the content of all the blob that were created on a particular day & will send an email with the content of the blob as an attachment. using the below work flow code you make the changes as per your business requirement.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Attach": {
"inputs": {
"variables": [
{
"name": "Arraytoattach",
"type": "array"
}
]
},
"runAfter": {
"Lists_blobs_(V2)": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"Blob_Name": {
"inputs": {
"variables": [
{
"name": "Count",
"type": "integer",
"value": "#length(variables('Arraytoattach'))"
}
]
},
"runAfter": {
"For_each": [
"Succeeded"
]
},
"type": "InitializeVariable"
},
"For_each": {
"actions": {
"Compose_2": {
"inputs": "#items('For_each')?['LastModified']",
"runAfter": {},
"type": "Compose"
},
"Compose_3": {
"inputs": "#formatDateTime(outputs('Compose_2'),'yyyy-MM-dd')",
"runAfter": {
"Compose_2": [
"Succeeded"
]
},
"type": "Compose"
},
"Condition": {
"actions": {
"Append_to_array_variable": {
"inputs": {
"name": "Arraytoattach",
"value": {
"ContentBytes": "#body('Get_blob_content_(V2)_2')?['$content']",
"Name": "#items('For_each')?['Name']"
}
},
"runAfter": {
"Get_blob_content_(V2)_2": [
"Succeeded"
]
},
"type": "AppendToArrayVariable"
},
"Get_blob_content_(V2)_2": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('stacklogictest'))}/files/#{encodeURIComponent(encodeURIComponent(items('For_each')?['Path']))}/content",
"queries": {
"inferContentType": true
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"expression": {
"and": [
{
"equals": [
"#outputs('Compose_3')",
"#formatDateTime(utcNow(),'yyyy-MM-dd')"
]
}
]
},
"runAfter": {
"Compose_3": [
"Succeeded"
]
},
"type": "If"
}
},
"foreach": "#body('Lists_blobs_(V2)')?['value']",
"runAfter": {
"Attach": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_blobs_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('stacklogictest'))}/foldersV2/#{encodeURIComponent(encodeURIComponent('JTJmdGVzdDEyMw=='))}",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"metadata": {
"JTJmdGVzdDEyMw==": "/test123"
},
"runAfter": {},
"type": "ApiConnection"
},
"Send_an_email_(V2)": {
"inputs": {
"body": {
"Attachments": "#variables('Arraytoattach')",
"Body": "<p>Total Number of blob created today : #{variables('Count')}</p>",
"Subject": "Blob created Today",
"To": "<**ReciepientMailAddress**>"
},
"host": {
"connection": {
"name": "#parameters('$connections')['office365']['connectionId']"
}
},
"method": "post",
"path": "/v2/Mail"
},
"runAfter": {
"Blob_Name": [
"Succeeded"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1,
"startTime": "2021-07-15T04:00:00Z"
},
"type": "Recurrence"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroups>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<subscriptionId>/providers/Microsoft.Web/locations/southindia/managedApis/azureblob"
},
"office365": {
"connectionId": "/subscriptions/<subscriptionId>/resourceGroups/<resourceGroups>/providers/Microsoft.Web/connections/office365",
"connectionName": "office365",
"id": "/subscriptions/<subscriptionId>/providers/Microsoft.Web/locations/southindia/managedApis/office365"
}
}
}
}
}
how to get all the blobs under a container, sub-folder and store the blobs metadata created as an array?
Ans: Using the Event Grid you can add all the blob under a particular container to a single array because event grid triggers the logic app when a new blob is added.
And then how to attach all the blobs created in a single email?
Ans: Due to outlook restrictions, we cannot sent or attach the data which excceds more than 25 MB.
3.How to get the blob name after, "When a resource event occurs" trigger?
Ans : As shown in the below image initialize a string variable and add it to the next of event grid event and add the below experssion to get the blob name from the event grid output
first(split(last(split(string(triggerBody()),'/blobs/')),'","event'))
The above logic app will fail if the content type of the blob is text you need make the changes accordingly based on requirement.
Here is the sample output of the above logic app
Created a logic app to send email with attachment of all the blobs inside a container that were added a day before the current day.
{
"definition": {
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"actions": {
"Current_time": {
"inputs": {},
"kind": "CurrentTime",
"runAfter": {
"Lists_blobs_(V2)": [
"Succeeded"
]
},
"type": "Expression"
},
"For_each": {
"actions": {
"For_each_2": {
"actions": {
"Condition": {
"actions": {
"Append_to_array_variable": {
"inputs": {
"name": "attachements",
"value": "#outputs('Compose')"
},
"runAfter": {
"Compose": [
"Succeeded"
]
},
"type": "AppendToArrayVariable"
},
"Compose": {
"inputs": {
"ContentBytes": "#{base64(body('Get_Blob'))}",
"Name": "#{last(split(items('For_each_2')?['Path'],'/'))}"
},
"runAfter": {
"Get_Blob": [
"Succeeded"
]
},
"type": "Compose"
},
"Get_Blob": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('<StorageAccountName/connection>'))}/files/#{encodeURIComponent(encodeURIComponent(items('For_each_2')?['Path']))}/content",
"queries": {
"inferContentType": true
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"expression": {
"and": [
{
"greater": [
"#items('For_each_2')?['LastModified']",
"#addDays(body('Current_time'),-1)"
]
}
]
},
"runAfter": {},
"type": "If"
}
},
"foreach": "#body('Lists_blobs_(V2)_3')?['value']",
"runAfter": {
"Lists_blobs_(V2)_3": [
"Succeeded"
]
},
"type": "Foreach"
},
"Lists_blobs_(V2)_3": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('<storageaccount connection>'))}/foldersV2/#{encodeURIComponent(encodeURIComponent(items('For_each')?['Path']))}",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"runAfter": {},
"type": "ApiConnection"
}
},
"foreach": "#body('Lists_blobs_(V2)')?['value']",
"runAfter": {
"Current_time": [
"Succeeded"
]
},
"type": "Foreach"
},
"Initialize_variable": {
"inputs": {
"variables": [
{
"name": "attachements",
"type": "array"
}
]
},
"runAfter": {},
"type": "InitializeVariable"
},
"Lists_blobs_(V2)": {
"inputs": {
"host": {
"connection": {
"name": "#parameters('$connections')['azureblob']['connectionId']"
}
},
"method": "get",
"path": "/v2/datasets/#{encodeURIComponent(encodeURIComponent('<storageaccount connection>'))}/foldersV2/#{encodeURIComponent(encodeURIComponent('JTJmY2FyYm9uYmxh='))}",
"queries": {
"nextPageMarker": "",
"useFlatListing": false
}
},
"metadata": {
"JTJmY2FyYm9uYmxh=": "/<containerName>"
},
"runAfter": {
"Initialize_variable": [
"Succeeded"
]
},
"type": "ApiConnection"
},
"Send_an_email_(V2)_3": {
"inputs": {
"body": {
"Attachments": "#variables('attachements')",
"Body": "<p>body</p>",
"Subject": "Subject",
"To": "Email"
},
"host": {
"connection": {
"name": "#parameters('$connections')['office365']['connectionId']"
}
},
"method": "post",
"path": "/v2/Mail"
},
"runAfter": {
"For_each": [
"Succeeded",
"Failed",
"Skipped",
"TimedOut"
]
},
"type": "ApiConnection"
}
},
"contentVersion": "1.0.0.0",
"outputs": {},
"parameters": {
"$connections": {
"defaultValue": {},
"type": "Object"
}
},
"triggers": {
"Recurrence": {
"recurrence": {
"frequency": "Day",
"interval": 1,
"schedule": {
"hours": [
"9"
],
"minutes": [
0
]
},
"timeZone": "Standard Time"
},
"type": "Recurrence"
}
}
},
"parameters": {
"$connections": {
"value": {
"azureblob": {
"connectionId": "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroup>/providers/Microsoft.Web/connections/azureblob",
"connectionName": "azureblob",
"id": "/subscriptions/<subscriptionId>/providers/Microsoft.Web/locations/<location>/managedApis/azureblob"
},
"office365": {
"connectionId": "/subscriptions/<subscriptionId>/resourceGroups/<ResourceGroup>/providers/Microsoft.Web/connections/office365-1",
"connectionName": "office365",
"id": "/subscriptions/subscriptionId>/providers/Microsoft.Web/locations/<location>/managedApis/office365"
}
}
}
}
}

Resources