c# - 由 Blob 存储上的 EventGrid 触发的 Azure 函数

标签 c# azure azure-functions azure-blob-storage azure-eventgrid

我已关注Microsoft tutorial处理基于 Azure 存储中创建的 blob 的事件。

事件正在触发,但由于 EventGrid 事件未填充输入流参数,因此处理图像的事件代码被绕过。这应该通过 blob(图像文件)的路径来处理。

 public static async Task Run(
        [EventGridTrigger]EventGridEvent eventGridEvent,
        [Blob("{data.url}", FileAccess.Read)] Stream input,
        ILogger log)
    {
        try
        {
            log.LogInformation("Entered Thumbnail Function ..");

            if (input != null) 
            { //doesn't get to here ..

每次事件触发时的日志是

2018-11-15T05:33:41.096 [Information] Executing 'Thumbnail' (Reason='EventGrid trigger fired at 2018-11-15T05:33:41.0781270+00:00' ..

2018-11-15T05:33:41.096 [Information] Entered Thumbnail Function

2018-11-15T05:33:41.096 [Information] Executed 'Thumbnail' (Succeeded, 

2018-11-15T05:33:41.096 [Information] Executing 'Thumbnail' (Reason='EventGrid trigger fired at 2018-11-15T05:33:41.0781270+00:00', 

2018-11-15T05:33:41.096 [Information] Entered Thumbnail Function

2018-11-15T05:33:41.096 [Information] Executed 'Thumbnail' (Succeeded,

最佳答案

本教程适用于 v1 c# script function正如你所看到的,它提到了 csx file当谈论功能代码时。但现在项目链接指向v2预编译代码,如果我们严格按照教程进行更改代码,可能会出现问题。

让我们通过两个步骤来解决不一致问题。

  1. 关键点是函数未连接到 part1 中创建的 blob 存储帐户,因此我们得到了空输入流。

    由于我们在 this step 中创建了应用设置 myblobstorage_STORAGE ,我们只需将它添加到我们的函数代码中即可。

    public static async Task Run(
        [EventGridTrigger]EventGridEvent eventGridEvent,
        [Blob("{data.url}", FileAccess.Read, Connection = "myblobstorage_STORAGE")] Stream input,
        ILogger log)
    
  2. 在同一步骤中,教程为 part1 中 Blob 存储帐户中创建的容器缩略图设置应用设置 myContainerName .

    但在我们的代码中我们可以看到它连接到 Storage account created for Function app使用 AzureWebJobsStorage 并希望从应用设置 THUMBNAIL_CONTAINER_NAME 获取容器名称。

    快速修复方法是替换 AzureWebJobsStorageTHUMBNAIL_CONTAINER_NAME,并为 thumbnailWidth 设置常量。

    private static readonly string BLOB_STORAGE_CONNECTION_STRING = Environment.GetEnvironmentVariable("myblobstorage_STORAGE");
    ...
    var thumbnailWidth = 100;
    var thumbContainerName = Environment.GetEnvironmentVariable("myContainerName");
    

    当然,您可以选择在Azure门户的应用程序设置中添加THUMBNAIL_WIDTH

重新发布,一切都应该正常。

关于c# - 由 Blob 存储上的 EventGrid 触发的 Azure 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53313094/

相关文章:

c# - 最好使用多线程? (线程池或线程)

c# - NHibernate 中的非锁定事务

c# - 处理作为独立进程运行的 zip 文件 Azure 函数

visual-studio - 为什么我的 Visual Studio Cloud 项目会递归地将其包嵌套在我的 Web 角色构建输出中?

amazon-web-services - azure 和 google 上的自定义联合代理

azure - 从 ADO 发布管道部署 azure 函数存档成功,但未创建函数

node.js - azure-functions-cli : func init spits error "No such file or directory"

c# - 无法从 C# 运行命令并捕获 StandardOutput

python - 加载 Python 3.8 Azure Function 的 azure-cosmos 库时出现问题

azure - 持久函数 - 事件函数内的可等待任务