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

标签 azure azure-blob-storage azure-logic-apps azure-eventgrid

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

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

enter image description here

我们想知道是否有办法收集容器下一天内添加的所有 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 限制,我们无法发送或附加超过 25 MB 的数据。

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

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

enter image description here

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

这是上述逻辑应用的示例输出 enter image description here

关于azure - 如何监视 Azure 存储容器/子文件夹中 Blob 的创建并触发逻辑应用发送电子邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68207551/

相关文章:

php - 如何在azure上创建服务器并在其上托管php、mysql站点

azure - 使用插入在 Azure sql 数据库中出现严重错误

c# - 有效的 Azure Blob 元数据标识符

c# - 使用 SDK (C#) 示例创建逻辑应用工作流

Azure 逻辑应用 - 内置连接器与托管连接器

Azure 逻辑应用 : Extract HTTP Header Key value into a variable

Azure API 应用程序对 WebJobs 的支持

sql-server - 通过 Azure 服务总线连接到本地 SQL Server

azure - 将标记与 Azure 存储列表 Blob 结合使用

azure - 在没有移动服务的情况下将图像上传到 Azure Blob 存储的体系结构