You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

添加Cosmos DB输出绑定后Event Grid无法触发Azure Function

问题分析与解决步骤

从你提供的日志和代码来看,添加Cosmos DB输出绑定后出现的NotFound错误并非Event Grid服务限制,而是配置和代码中的问题导致函数无法正常启动或触发:

1. 移除多余的Http输出绑定

你的函数是Event Grid触发,而非Http触发,绑定配置中多余的Http输出绑定会干扰函数的触发逻辑。修改function.json,删除Http输出绑定块:

{
  "scriptFile": "func.py",
  "bindings": [
    {
      "type": "eventGridTrigger",
      "name": "event_in",
      "direction": "in"
    },
    {
      "type": "cosmosDB",
      "direction": "out",
      "name": "doc",
      "databaseName": "MyDb",
      "collectionName": "my-files",
      "createIfNotExists": "true",
      "connectionStringSetting": "AzureCosmosDBConnectionString"
    }
  ]
}

同时修改代码,去掉返回func.HttpResponse的逻辑——Event Grid触发的函数无需返回Http响应:

import logging
import json
import azure.functions as func

def main(event_in: func.EventGridEvent,  doc: func.Out[func.Document]) -> None:
    result = json.dumps({
        'id': event_in.id,
        'data': event_in.get_json(),
        'topic': event_in.topic,
        'subject': event_in.subject,
        'event_type': event_in.event_type,
    })

    logging.info('Python EventGrid trigger processed an event: %s', result)

    # 如果需要写入Cosmos DB,添加以下代码
    doc.set(func.Document.from_json(result))

2. 确认Cosmos DB连接字符串配置正确

检查函数应用的应用设置(Azure门户 -> 函数应用 -> 配置 -> 应用设置),确保AzureCosmosDBConnectionString已正确配置,值为Cosmos DB账户的完整连接字符串(从Cosmos DB账户的“密钥”页面复制)。

如果连接字符串错误,函数启动时会初始化Cosmos DB绑定失败,导致函数实例无法正常运行,Event Grid请求时就会返回NotFound。

3. 确保函数部署成功且状态正常

修改代码和绑定后,重新部署函数应用,确认部署状态为“成功”。在Azure门户的函数页面,查看目标函数的状态为“已启用”,无启动错误。

4. 验证Event Grid订阅的目标端点

确认Event Grid事件订阅的目标函数端点正确,指向你的Event Grid触发函数的端点(而非Http触发函数)。通常函数部署后端点不会变更,但如果有多个函数,需确保订阅的是正确的函数资源ID。


内容的提问来源于stack exchange,提问作者fandro

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.30 08:25:31