谁能帮我解决以下错误:
结果:失败异常:ImportError:无法从“azure.eventgrid”导入名称“EventGridPublisherClient”
这是HTTPTrigger的代码:
import os
import logging
from azure.core.credentials import AzureKeyCredential
from azure.eventgrid import EventGridPublisherClient, EventGridEvent
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
logging.info('Python HTTP trigger function processed a request.')
# Parse request body
#req_body = req.get_json()
# Get Event Grid topic endpoint and key from environment variables
topic_endpoint = os.environ.get('EVENT_GRID_TOPIC_ENDPOINT')
topic_key = os.environ.get('EVENT_GRID_TOPIC_KEY')
# Create Event Grid publisher client
credential = AzureKeyCredential(topic_key)
client = EventGridPublisherClient(topic_endpoint, credential)
# Create an event
event = EventGridEvent(
event_type="MyCustomEventType",
subject="MyCustomSubject",
data={
"message": "Hello, Event Grid!"
},
data_version="1.0"
)
# Publish the event
client.send(event)
# Return a response
return func.HttpResponse("Event published to Event Grid topic.", status_code=200)
requirements.txt看起来像这样:
urllib3
uplink
requests
azure-functions
azure
azure-eventgrid
azure-core
在本地运行可以正常工作,但是当我部署到Azure时,我得到了上面的错误。我在绕圈子,似乎找不到任何有用的信息来帮助这一点。有人有主意吗?
1条答案
按热度按时间nx7onnlm1#
我尝试使用以下代码部署Azure Function HTTP触发器,并成功。
代码:
local.setting.json:
requirement.txt:
我在azure portal的Configurationfunctionapp的Application.settings中添加了EVENT_GRID_TOPIC_ENDPOINT和eventgridtopickey,
我运行上面的代码,得到了以下结果:
通过上面的URL,我可以在浏览器中看到如下输出:
然后我将上面的代码部署到functionapp**中,如下所示:
选择要部署的functionapp,
点击Delpoy选项,
HTTP触发函数部署成功,点击查看输出,
输出显示HTTP trigger函数成功部署到functionapp,如下所示:
已成功将部署到Azure portal中的functionapp,如下所示: