如何监控 Azure 存储 container/subfolders 中 Blob 的创建并触发逻辑应用程序发送电子邮件

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

我们有 Azure 存储容器,其中包含动态创建的子文件夹,我们希望在此 container/subfolders 下监控 blob 的创建,并每天触发一封电子邮件,将当天添加的所有 blob 作为附件添加到电子邮件。

我们尝试的是创建一个带有事件网格触发器(当资源事件发生时)的逻辑应用程序,如下所示。

我们想知道是否有办法收集容器下一天内添加的所有 blob 并获取所有这些 blob 的内容并将其添加为附件并在预定时间触发电子邮件一次天.

关于

的任何建议
  1. 如何获取容器、子文件夹下的所有 blob 并将创建的 blob 元数据存储为数组?
  2. 然后如何附加在一封电子邮件中创建的所有 blob?
  3. 如何在“资源事件发生”触发后获取 blob 名称?

根据上述要求,我们创建了一个使用计时器的逻辑应用程序,用于列出和获取在特定日期创建的所有 blob 的内容,并将以 blob 的内容作为附件发送一封电子邮件.使用下面的工作流程代码,您可以根据您的业务需求进行更改。

{
    "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"
                }
            }
        }
    }
}
  1. how to get all the blobs under a container, sub-folder and store the blobs metadata created as an array?

回答:使用事件网格,您可以将特定容器下的所有 blob 添加到单个数组,因为事件网格会在添加新 blob 时触发逻辑应用。

  1. And then how to attach all the blobs created in a single email?

答:由于outlook的限制,我们无法发送或附加超过25MB的数据。

3.How to get the blob name after, "When a resource event occurs" trigger?

答案:如下图所示,初始化一个字符串变量并将其添加到事件网格事件的下一个,并添加以下表达式以从事件网格输出中获取 blob 名称 第一个(拆分(最后(拆分(字符串(triggerBody()),'/ blobs /')),'“,”事件'))

如果 blob 的内容类型是文本,上述逻辑应用将失败,您需要根据需要进行相应的更改。

这是上述逻辑应用程序的示例输出

创建了一个逻辑应用来发送电子邮件,其中包含容器内所有 blob 的附件,这些 blob 是在当天前一天添加的。

{
"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"
            }
        }
    }
}

}