適用於 Python 的 Event Grid 程式庫Event Grid libraries for Python
Azure Event Grid 是完全受控的智慧型事件路由服務,可讓統一事件耗用量使用發佈-訂閱模型。Azure Event Grid is a fully-managed intelligent event routing service that allows for uniform event consumption using a publish-subscribe model.
進一步了解 Azure Event Grid 的相關資訊,並參考 Azure Blob 儲存體教學課程開始使用。Learn more about Azure Event Grid and get started with the Azure Blob storage event tutorial.
發行 SDKPublish SDK
使用 Azure Event Grid 發佈 SDK 來驗證、建立、處理事件,並將事件發佈至主題。Authenticate, create, handle, and publish events to topics using the Azure Event Grid publish SDK.
安裝Installation
使用 pip 安裝套件:Install the package with pip:
pip install azure-eventgrid
範例Example
下列程式碼會將事件發佈至主題。The following code publishes an event to a topic. 您可以透過 Azure 入口網站或 Azure CLI,擷取主題金鑰及端點:You can retrieve the topic key and endpoint through the Azure Portal or through the Azure CLI:
endpoint=$(az eventgrid topic show --name <topic_name> -g gridResourceGroup --query "endpoint" --output tsv)
key=$(az eventgrid topic key list --name <topic_name> -g gridResourceGroup --query "key1" --output tsv)
from datetime import datetime
from azure.eventgrid import EventGridClient
from msrest.authentication import TopicCredentials
def publish_event(self):
credentials = TopicCredentials(
self.settings.EVENT_GRID_KEY
)
event_grid_client = EventGridClient(credentials)
event_grid_client.publish_events(
"your-endpoint-here",
events=[{
'id' : "dbf93d79-3859-4cac-8055-51e3b6b54bea",
'subject' : "Sample subject",
'data': {
'key': 'Sample Data'
},
'event_type': 'SampleEventType',
'event_time': datetime(2018, 5, 2),
'data_version': 1
}]
)
管理 SDKManagement SDK
透過管理 SDK 來建立、更新或刪除 Event Grid 執行個體、主題和訂用帳戶。Create, update, or delete Event Grid instances, topics, and subscriptions with the management SDK.
安裝Installation
使用 pip 安裝套件:Install the package with pip:
pip install azure-mgmt-eventgrid
範例Example
下列程式碼會建立一個自訂主題,並訂閱主題端點。The following creates a custom topic and subscribes an endpoint to the topic. 接著,程式碼會透過 HTTPS 將事件傳送至主題。The code then sends an event to the topic through HTTPS. RequestBin 是一個開放原始碼的第三方工具,可讓您建立端點,以及檢視傳送給它的要求。RequestBin is an open source, third-party tool that enables you to create an endpoint, and view requests that are sent to it. 移至 RequestBin,然後按一下 [建立 RequestBin] 。Go to RequestBin, and click Create a RequestBin. 複製 bin URL,因為您在訂閱主題時需要用到它。Copy the bin URL, because you need it when subscribing to the topic.
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.eventgrid import EventGridManagementClient
import requests
RESOURCE_GROUP_NAME = 'gridResourceGroup'
TOPIC_NAME = 'gridTopic1234'
LOCATION = 'westus2'
SUBSCRIPTION_ID = 'YOUR_SUBSCRIPTION_ID'
SUBSCRIPTION_NAME = 'gridSubscription'
REQUEST_BIN_URL = 'YOUR_REQUEST_BIN_URL'
# create resource group
resource_client = ResourceManagementClient(credentials, SUBSCRIPTION_ID)
resource_client.resource_groups.create_or_update(
RESOURCE_GROUP_NAME,
{
'location': LOCATION
}
)
event_client = EventGridManagementClient(credentials, SUBSCRIPTION_ID)
# create a custom topic
event_client.topics.create_or_update(RESOURCE_GROUP_NAME, TOPIC_NAME, LOCATION)
# subscribe to a topic
scope = '/subscriptions/'+SUBSCRIPTION_ID+'/resourceGroups/'+RESOURCE_GROUP_NAME+'/providers/Microsoft.EventGrid/topics/'+TOPIC_NAME
event_client.event_subscriptions.create(scope, SUBSCRIPTION_NAME,
{
'destination': {
'endpoint_url': REQUEST_BIN_URL
}
}
)
# send an event to topic
# get endpoint url
url = event_client.event_subscriptions.get_full_url(scope, SUBSCRIPTION_NAME).endpoint_url
# get key
key = event_client.topics.list_shared_access_keys(RESOURCE_GROUP_NAME,TOPIC_NAME).key1
headers = {'aeg-sas-key': key}
s = requests.get('https://raw.githubusercontent.com/Azure/azure-docs-json-samples/master/event-grid/customevent.json')
r = requests.post(url, data=s, headers=headers)
print(r.status_code)
print(r.content)
瀏覽至先前所建立的 RequestBin URL 可看到剛傳送的事件。Browse to the RequestBin URL created earlier to see the event just sent.
清除資源Clean up resources
az group delete --name gridResourceGroup
深入了解Learn more
使用 Event Grid SDK 接收事件Receive events using the Event Grid SDK