如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

huangapple go评论105阅读模式
英文:

How to fix ImportError with 'EventGridPublisherClient' in Azure HTTPTrigger function?

问题

> 结果:失败 异常:ImportError:无法从'azure.eventgrid'导入名称'EventGridPublisherClient'。

这是HTTP触发器的代码:

  1. import os
  2. import logging
  3. from azure.core.credentials import AzureKeyCredential
  4. from azure.eventgrid import EventGridPublisherClient, EventGridEvent
  5. import azure.functions as func
  6. def main(req: func.HttpRequest) -> func.HttpResponse:
  7. logging.info('Python HTTP触发器函数处理了一个请求。')
  8. # 解析请求体
  9. # req_body = req.get_json()
  10. # 从环境变量中获取Event Grid主题端点和密钥
  11. topic_endpoint = os.environ.get('EVENT_GRID_TOPIC_ENDPOINT')
  12. topic_key = os.environ.get('EVENT_GRID_TOPIC_KEY')
  13. # 创建Event Grid发布者客户端
  14. credential = AzureKeyCredential(topic_key)
  15. client = EventGridPublisherClient(topic_endpoint, credential)
  16. # 创建事件
  17. event = EventGridEvent(
  18. event_type="MyCustomEventType",
  19. subject="MyCustomSubject",
  20. data={
  21. "message": "Hello, Event Grid!"
  22. },
  23. data_version="1.0"
  24. )
  25. # 发布事件
  26. client.send(event)
  27. # 返回响应
  28. return func.HttpResponse("事件已发布到Event Grid主题。", status_code=200)

requirements.txt文件如下:

  1. urllib3
  2. uplink
  3. requests
  4. azure-functions
  5. azure
  6. azure-eventgrid
  7. 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:

  1. import os
  2. import logging
  3. from azure.core.credentials import AzureKeyCredential
  4. from azure.eventgrid import EventGridPublisherClient, EventGridEvent
  5. import azure.functions as func
  6. def main(req: func.HttpRequest) -> func.HttpResponse:
  7. logging.info('Python HTTP trigger function processed a request.')
  8. # Parse request body
  9. #req_body = req.get_json()
  10. # Get Event Grid topic endpoint and key from environment variables
  11. topic_endpoint = os.environ.get('EVENT_GRID_TOPIC_ENDPOINT')
  12. topic_key = os.environ.get('EVENT_GRID_TOPIC_KEY')
  13. # Create Event Grid publisher client
  14. credential = AzureKeyCredential(topic_key)
  15. client = EventGridPublisherClient(topic_endpoint, credential)
  16. # Create an event
  17. event = EventGridEvent(
  18. event_type="MyCustomEventType",
  19. subject="MyCustomSubject",
  20. data={
  21. "message": "Hello, Event Grid!"
  22. },
  23. data_version="1.0"
  24. )
  25. # Publish the event
  26. client.send(event)
  27. # Return a response
  28. return func.HttpResponse("Event published to Event Grid topic.", status_code=200)

The requirements.txt looks like this:

  1. urllib3
  2. uplink
  3. requests
  4. azure-functions
  5. azure
  6. azure-eventgrid
  7. 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:

  1. import os
  2. import logging
  3. from azure.core.credentials import AzureKeyCredential
  4. from azure.eventgrid import EventGridPublisherClient, EventGridEvent
  5. import azure.functions as func
  6. def main(req: func.HttpRequest) -> func.HttpResponse:
  7. logging.info('Python HTTP trigger function processed a request.')
  8. topic_endpoint = os.environ.get('EVENT_GRID_TOPIC_ENDPOINT')
  9. topic_key = os.environ.get('EVENT_GRID_TOPIC_KEY')
  10. credential = AzureKeyCredential(topic_key)
  11. client = EventGridPublisherClient(topic_endpoint, credential)
  12. event = EventGridEvent(
  13. event_type="MyCustomEventType",
  14. subject="MyCustomSubject",
  15. data={
  16. "message": "Hello, Event Grid!"
  17. },
  18. data_version="1.0"
  19. )
  20. client.send(event)
  21. return func.HttpResponse("Event published to Event Grid topic.", status_code=200)

local.setting.json:

  1. {
  2. "IsEncrypted": false,
  3. "Values": {
  4. "AzureWebJobsStorage": "<your-storage-account-connection-string>",
  5. "FUNCTIONS_WORKER_RUNTIME": "python",
  6. "EVENT_GRID_TOPIC_KEY": "<your-event-grid-topic-key>",
  7. "EVENT_GRID_TOPIC_ENDPOINT": "<your-event-grid-topic-endpoint>"
  8. }
  9. }

requirement.txt:

  1. azure-functions
  2. azure-eventgrid==4.0.0
  3. azure-core>=1.18.0

I added EVENT_GRID_TOPIC_ENDPOINT and eventgridtopickey in Application.settings in Configuration functionapp at azure portal,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

I run the above code and got below results:

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

With the above URL, I can able to see output at the browser as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Then, I deployed above code to functionapp as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Select the functionapp that you want to deploy,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Click on Deploy option,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

The HTTP trigger function deployed successfully,
Click on View output,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

The output shows that the HTTP trigger function is successfully deployed to functionapp as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Successfully deployed to functionapp in Azure portal as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

英文:

I tried to Deploy Azure Function HTTP Trigger with the below code and it was successful.

Code:

  1. import os
  2. import logging
  3. from azure.core.credentials import AzureKeyCredential
  4. from azure.eventgrid import EventGridPublisherClient, EventGridEvent
  5. import azure.functions as func
  6. def main(req: func.HttpRequest) -&gt; func.HttpResponse:
  7. logging.info(&#39;Python HTTP trigger function processed a request.&#39;)
  8. topic_endpoint = os.environ.get(&#39;EVENT_GRID_TOPIC_ENDPOINT&#39;)
  9. topic_key = os.environ.get(&#39;EVENT_GRID_TOPIC_KEY&#39;)
  10. credential = AzureKeyCredential(topic_key)
  11. client = EventGridPublisherClient(topic_endpoint, credential)
  12. event = EventGridEvent(
  13. event_type=&quot;MyCustomEventType&quot;,
  14. subject=&quot;MyCustomSubject&quot;,
  15. data={
  16. &quot;message&quot;: &quot;Hello, Event Grid!&quot;
  17. },
  18. data_version=&quot;1.0&quot;
  19. )
  20. client.send(event)
  21. return func.HttpResponse(&quot;Event published to Event Grid topic.&quot;, status_code=200)

local.setting.json:

  1. {
  2. &quot;IsEncrypted&quot;: false,
  3. &quot;Values&quot;: {
  4. &quot;AzureWebJobsStorage&quot;: &quot;&lt;your-storage-account-connection-string&gt;&quot;,
  5. &quot;FUNCTIONS_WORKER_RUNTIME&quot;: &quot;python&quot;,
  6. &quot;EVENT_GRID_TOPIC_KEY&quot;: &quot;&lt;your-event-grid-topic-key&gt;&quot;,
  7. &quot;EVENT_GRID_TOPIC_ENDPOINT&quot;: &quot;&lt;your-event-grid-topic-endpoint&gt;&quot;
  8. }
  9. }

requirement.txt:

  1. azure-functions
  2. azure-eventgrid==4.0.0
  3. azure-core&gt;=1.18.0

I added EVENT_GRID_TOPIC_ENDPOINT and eventgridtopickey in Application.settings in Configuration functionapp at azure portal,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

I run the above code and got below results:

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

With the above URL, I can able to see output at browser as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Then, I deployed above code to functionapp as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Select the functionapp that you want to deploy,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Click on Delpoy option,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

The HTTP trigger function deployed successfully,
Click on View output,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

The output shows that the HTTP trigger function is successfully deployed to functionapp as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

Successfully deployed to functionapp in Azure portal as below,

如何在Azure HTTP 触发器函数中修复对 ‘EventGridPublisherClient’ 的 ImportError?

huangapple
  • 本文由 发表于 2023年5月28日 12:11:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76349904.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定