如何监视 Azure 存储容器/子文件夹中 Blob 的创建并触发逻辑应用发送电子邮件

Posted

技术标签:

【中文标题】如何监视 Azure 存储容器/子文件夹中 Blob 的创建并触发逻辑应用发送电子邮件【英文标题】:How to monitor the creation of Blob in Azure Storage container/subfolders and Trigger a logic app to send email 【发布时间】:2021-09-13 08:48:38 【问题描述】:

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

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

我们想知道是否有方法可以收集容器下一天中添加的所有 blob,并获取所有这些 blob 的内容并将其添加为附件,并每天在预定时间触发一封电子邮件。

任何建议

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

【问题讨论】:

【参考方案1】:

基于上述要求,我们创建了一个逻辑应用程序,使用计时器来列出和获取在特定日期创建的所有 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"
                
            
        
    

    如何获取容器、子文件夹下的所有 blob 并将创建的 blob 元数据存储为数组?

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

    然后如何附加在一封电子邮件中创建的所有 blob?

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

3.“当资源事件发生时”触发后如何获取blob名称?

Ans : 如下图所示初始化一个字符串变量并将其添加到事件网格事件的下一个并添加以下表达式以从事件网格输出中获取 blob 名称 first(split(last(split(string(triggerBody()),'/blob/')),'","event')))

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

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

【讨论】:

【参考方案2】:

创建了一个逻辑应用来发送电子邮件,其中包含容器内在当天前一天添加的所有 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"
            
        
    

【讨论】:

以上是关于如何监视 Azure 存储容器/子文件夹中 Blob 的创建并触发逻辑应用发送电子邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Azure 的子容器中获取 Blob 列表

在 azure blob 子容器中从本地上传 zip

如何在 Azure 存储容器中创建目录而不创建额外文件?

使用 ADF 将文件夹从具有 2 级子文件夹的 azure 容器移动到与子文件夹同名的容器级

如何监视对 Azure 存储帐户防火墙规则所做的更改并发出警报

C# Azure.Storage.Blobs SDK 如何列出和压缩容器中的所有文件并将压缩文件存储在另一个容器中