英文:
How to fix ImportError with 'EventGridPublisherClient' in Azure HTTPTrigger function?
问题
> 结果:失败 异常:ImportError:无法从'azure.eventgrid'导入名称'EventGridPublisherClient'。
这是HTTP触发器的代码:
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触发器函数处理了一个请求。')
# 解析请求体
# req_body = req.get_json()
# 从环境变量中获取Event Grid主题端点和密钥
topic_endpoint = os.environ.get('EVENT_GRID_TOPIC_ENDPOINT')
topic_key = os.environ.get('EVENT_GRID_TOPIC_KEY')
# 创建Event Grid发布者客户端
credential = AzureKeyCredential(topic_key)
client = EventGridPublisherClient(topic_endpoint, credential)
# 创建事件
event = EventGridEvent(
event_type="MyCustomEventType",
subject="MyCustomSubject",
data={
"message": "Hello, Event Grid!"
},
data_version="1.0"
)
# 发布事件
client.send(event)
# 返回响应
return func.HttpResponse("事件已发布到Event Grid主题。", status_code=200)
requirements.txt文件如下:
urllib3
uplink
requests
azure-functions
azure
azure-eventgrid
azure-core
在本地运行正常,但在部署到Azure时出现上述错误。我一直在循环中,似乎找不到任何有用的信息来帮助解决这个问题。有人有什么想法吗?
英文:
Can anyone help me with the following error:
> Result: Failure Exception: ImportError: cannot import name 'EventGridPublisherClient' from 'azure.eventgrid'
This is the code for the 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)
The requirements.txt looks like this:
urllib3
uplink
requests
azure-functions
azure
azure-eventgrid
azure-core
Running locally works fine but when I deploy to Azure I get the error above. I'm going in circles and don't seem to find any useful information to help with this. Anyone has an idea?
答案1
得分: 1
I tried to Deploy Azure Function HTTP Trigger with the below code and it was successful.
Code:
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.')
topic_endpoint = os.environ.get('EVENT_GRID_TOPIC_ENDPOINT')
topic_key = os.environ.get('EVENT_GRID_TOPIC_KEY')
credential = AzureKeyCredential(topic_key)
client = EventGridPublisherClient(topic_endpoint, credential)
event = EventGridEvent(
event_type="MyCustomEventType",
subject="MyCustomSubject",
data={
"message": "Hello, Event Grid!"
},
data_version="1.0"
)
client.send(event)
return func.HttpResponse("Event published to Event Grid topic.", status_code=200)
local.setting.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<your-storage-account-connection-string>",
"FUNCTIONS_WORKER_RUNTIME": "python",
"EVENT_GRID_TOPIC_KEY": "<your-event-grid-topic-key>",
"EVENT_GRID_TOPIC_ENDPOINT": "<your-event-grid-topic-endpoint>"
}
}
requirement.txt:
azure-functions
azure-eventgrid==4.0.0
azure-core>=1.18.0
I added EVENT_GRID_TOPIC_ENDPOINT and eventgridtopickey in Application.settings in Configuration functionapp at azure portal,
I run the above code and got below results:
With the above URL, I can able to see output at the browser as below,
Then, I deployed above code to functionapp as below,
Select the functionapp that you want to deploy,
Click on Deploy option,
The HTTP trigger function deployed successfully,
Click on View output,
The output shows that the HTTP trigger function is successfully deployed to functionapp as below,
Successfully deployed to functionapp in Azure portal as below,
英文:
I tried to Deploy Azure Function HTTP Trigger with the below code and it was successful.
Code:
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.')
topic_endpoint = os.environ.get('EVENT_GRID_TOPIC_ENDPOINT')
topic_key = os.environ.get('EVENT_GRID_TOPIC_KEY')
credential = AzureKeyCredential(topic_key)
client = EventGridPublisherClient(topic_endpoint, credential)
event = EventGridEvent(
event_type="MyCustomEventType",
subject="MyCustomSubject",
data={
"message": "Hello, Event Grid!"
},
data_version="1.0"
)
client.send(event)
return func.HttpResponse("Event published to Event Grid topic.", status_code=200)
local.setting.json:
{
"IsEncrypted": false,
"Values": {
"AzureWebJobsStorage": "<your-storage-account-connection-string>",
"FUNCTIONS_WORKER_RUNTIME": "python",
"EVENT_GRID_TOPIC_KEY": "<your-event-grid-topic-key>",
"EVENT_GRID_TOPIC_ENDPOINT": "<your-event-grid-topic-endpoint>"
}
}
requirement.txt:
azure-functions
azure-eventgrid==4.0.0
azure-core>=1.18.0
I added EVENT_GRID_TOPIC_ENDPOINT and eventgridtopickey in Application.settings in Configuration functionapp at azure portal,
I run the above code and got below results:
With the above URL, I can able to see output at browser as below,
Then, I deployed above code to functionapp as below,
Select the functionapp that you want to deploy,
Click on Delpoy option,
The HTTP trigger function deployed successfully,
Click on View output,
The output shows that the HTTP trigger function is successfully deployed to functionapp as below,
Successfully deployed to functionapp in Azure portal as below,
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论