使用 Azure CLI 订阅 Blob 存储帐户的事件Subscribe to events for a Blob storage account with Azure CLI

此脚本创建 Blob 存储帐户事件的事件网格订阅。This script creates an Event Grid subscription to the events for a Blob storage account.

若要运行此示例,请安装最新版本的 Azure CLITo run this sample, install the latest version of the Azure CLI. 若要开始,请运行 az login 以创建与 Azure 的连接。To start, run az login to create a connection with Azure.

适用于 Azure CLI 的示例是针对 bash shell 编写的。Samples for the Azure CLI are written for the bash shell. 若要在 Windows PowerShell 或命令提示符中运行此示例,可能需要更改脚本的元素。To run this sample in Windows PowerShell or Command Prompt, you may need to change elements of the script.

如果没有 Azure 订阅,可在开始前创建一个试用帐户If you don't have an Azure subscription, create a trial account before you begin.

示例脚本Sample script

#!/bin/bash

# You must have the latest version of the Azure CLI

# Provide a unique name for the Blob storage account.
storageName="<Name of the storage account>"

# Provide an endpoint for handling the events.
myEndpoint="<endpoint URL>"

# Provide the name of the resource group to contain the storage account.
myResourceGroup="<resource group name>"

# Select the Azure subscription to contain the storage account and event subscription.
az account set --subscription "<name or ID of the subscription>"

# Create resource group
az group create --name $myResourceGroup --location chinanorth2

# Create the Blob storage account. 
az storage account create \
  --name $storageName \
  --location chinanorth2 \
  --resource-group $myResourceGroup \
  --sku Standard_LRS \
  --kind BlobStorage \
  --access-tier Hot

# Get the resource ID of the Blob storage account.
storageid=$(az storage account show --name $storageName --resource-group $myResourceGroup --query id --output tsv)

# Subscribe to the Blob storage account. 
az eventgrid event-subscription create \
  --source-resource-id $storageid \
  --name demoSubToStorage \
  --endpoint $myEndpoint

脚本说明Script explanation

此脚本使用以下命令创建事件订阅。This script uses the following command to create the event subscription. 表中的每条命令链接到特定于命令的文档。Each command in the table links to command-specific documentation.

CommandCommand 说明Notes
az eventgrid event-subscription createaz eventgrid event-subscription create 创建事件网格订阅。Create an Event Grid subscription.
az eventgrid event-subscription create - 扩展版本az eventgrid event-subscription create - extension version 创建事件网格订阅。Create an Event Grid subscription.

后续步骤Next steps