快速入門:使用 Azure CLI 將儲存體事件路由至 Web 端點

Azure Event Grid 是一項雲端事件服務。 在本文中,您可使用 Azure CLI 訂閱 Blob 儲存體事件,以及觸發事件來檢視結果。

通常,您會將事件傳送至可處理事件資料及採取行動的端點。 不過,若要簡化這篇文章,您可將事件傳送至可收集及顯示訊息的 Web 應用程式。

當您完成本文所述的步驟時,會看到事件資料已傳送至 Web 應用程式。

Screenshot of the Azure Event Grid Viewer that shows event data that has been sent to the web app.

如果您沒有 Azure 訂閱,請在開始之前,先建立 Azure 免費帳戶

必要條件

  • 本文需要 2.0.70 版或更新版本的 Azure CLI。 如果您是使用 Azure Cloud Shell,就已安裝最新版本。

建立資源群組

Event Grid 為 Azure 資源,必須放入 Azure 資源群組中。 資源群組是在其中部署與管理 Azure 資源的邏輯集合。

使用 az group create 命令來建立資源群組。

下列範例會在 westcentralus 位置建立名為 <resource_group_name> 的資源群組。 以資源群組的唯一名稱取代 <resource_group_name>

az group create --name <resource_group_name> --location westcentralus

建立儲存體帳戶

您可以在一般用途 v2 儲存體帳戶與 Blob 儲存體帳戶中找到 Blob 儲存體事件。 一般用途 v2 儲存體帳戶支援所有儲存體服務 (包括 Blob、檔案、佇列和表格) 的所有功能。 Blob 儲存體帳戶是特製化的儲存體帳戶,可將非結構化資料儲存為 Azure 儲存體中的 Blob (物件)。 Blob 儲存體帳戶類似於一般用途儲存體帳戶,可共用所有強大的持續性、可用性、延展性以及您現今使用的效能功能,包括區塊 Blob 和附加 Blob 的 100% API 一致性。 如需詳細資訊,請參閱 Azure 儲存體帳戶概觀

以儲存體帳戶的唯一名稱取代 <storage_account_name>,並以您稍早建立的資源群組取代 <resource_group_name>

az storage account create \
  --name <storage_account_name> \
  --location westcentralus \
  --resource-group <resource_group_name> \
  --sku Standard_LRS \
  --kind BlobStorage \
  --access-tier Hot

建立訊息端點

訂閱主題之前,讓我們建立事件訊息的端點。 端點通常會根據事件資料來採取動作。 若要簡化此快速入門,請部署預先建置的 Web 應用程式以顯示事件訊息。 部署的解決方案包括 App Service 方案、App Service Web 應用程式,以及 GitHub 的原始程式碼。

以 Web 應用程式的唯一名稱取代 <your-site-name>。 Web 應用程式名稱必須是唯一的,因為它是 DNS 項目的一部分。

sitename=<your-site-name>

az deployment group create \
  --resource-group <resource_group_name> \
  --template-uri "https://raw.githubusercontent.com/Azure-Samples/azure-event-grid-viewer/master/azuredeploy.json" \
  --parameters siteName=$sitename hostingPlanName=viewerhost

部署需要幾分鐘的時間才能完成。 成功部署之後,檢視 Web 應用程式,確定它正在執行。 在網頁瀏覽器中,瀏覽至:https://<your-site-name>.azurewebsites.net

您現在應該會看到沒有顯示任何訊息的網站。

啟用事件方格資源提供者

  1. 如果您先前在 Azure 訂用帳戶中從未使用過事件方格,可能必須註冊事件方格資源提供者。 執行下列命令以註冊提供者:

    az provider register --namespace Microsoft.EventGrid
    
  2. 可能需要一點時間才能完成註冊。 若要檢查狀態,請執行下列命令:

    az provider show --namespace Microsoft.EventGrid --query "registrationState"
    

    registrationStateRegistered 時,代表已準備好繼續進行。

訂閱您的儲存體帳戶

您可訂閱主題,告知 Event Grid 您想要追蹤的事件,以及要將事件傳送至何處。 下列範例會訂閱您所建立的儲存體帳戶,並從 Web 應用程式傳遞 URL 作為事件通知的端點。 以事件訂用帳戶的名稱來取代 <event_subscription_name>。 對於 <resource_group_name><storage_account_name>,使用您稍早建立的值。

Web 應用程式的端點必須包含的尾碼 /api/updates/

storageid=$(az storage account show --name <storage_account_name> --resource-group <resource_group_name> --query id --output tsv)
endpoint=https://$sitename.azurewebsites.net/api/updates

az eventgrid event-subscription create \
  --source-resource-id $storageid \
  --name <event_subscription_name> \
  --endpoint $endpoint

再次檢視您的 Web 應用程式,並注意訂閱驗證事件是否已傳送至其中。 選取眼睛圖示以展開事件資料。 事件方格會傳送驗證事件,以便端點確認接收事件資料。 Web 應用程式包括用於驗證訂閱的程式碼。

View subscription event

從 Blob 儲存體觸發事件

現在,讓我們觸發事件以了解 Event Grid 如何將訊息散發至您的端點。 首先,我們要設定儲存體帳戶的名稱和金鑰,然後建立容器,接著建立並上傳檔案。 同樣地,使用您稍早建立之 <storage_account_name><resource_group_name> 的值。

export AZURE_STORAGE_ACCOUNT=<storage_account_name>
export AZURE_STORAGE_KEY="$(az storage account keys list --account-name <storage_account_name> --resource-group <resource_group_name> --query "[0].value" --output tsv)"

az storage container create --name testcontainer

touch testfile.txt
az storage blob upload --file testfile.txt --container-name testcontainer --name testfile.txt

您已觸發此事件,而 Event Grid 會將訊息傳送至您在訂閱時設定的端點。 檢視您的 Web 應用程式,以查看您剛剛傳送的事件。

[{
  "topic": "/subscriptions/xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/myrg/providers/Microsoft.Storage/storageAccounts/myblobstorageaccount",
  "subject": "/blobServices/default/containers/testcontainer/blobs/testfile.txt",
  "eventType": "Microsoft.Storage.BlobCreated",
  "eventTime": "2017-08-16T20:33:51.0595757Z",
  "id": "4d96b1d4-0001-00b3-58ce-16568c064fab",
  "data": {
    "api": "PutBlockList",
    "clientRequestId": "d65ca2e2-a168-4155-b7a4-2c925c18902f",
    "requestId": "4d96b1d4-0001-00b3-58ce-16568c000000",
    "eTag": "0x8D4E4E61AE038AD",
    "contentType": "text/plain",
    "contentLength": 0,
    "blobType": "BlockBlob",
    "url": "https://myblobstorageaccount.blob.core.windows.net/testcontainer/testfile.txt",
    "sequencer": "00000000000000EB0000000000046199",
    "storageDiagnostics": {
      "batchId": "dffea416-b46e-4613-ac19-0371c0c5e352"
    }
  },
  "dataVersion": "",
  "metadataVersion": "1"
}]

清除資源

如果您打算繼續使用此儲存體帳戶和事件訂用帳戶,請勿清除在本文中建立的資源。 如果您不打算繼續,請使用下列命令來刪除您在本文建立的資源。

以您在上面建立的資源群組取代 <resource_group_name>

az group delete --name <resource_group_name>

下一步

您現在知道如何建立主題和事件訂用帳戶,請深入了解 Blob 儲存體事件以及 Event Grid 如何協助您: