您现在访问的是微软AZURE全球版技术文档网站,若需要访问由世纪互联运营的MICROSOFT AZURE中国区技术文档网站,请访问 https://docs.azure.cn.

教程:使用事件网格自动调整上传图像的大小Tutorial: Automate resizing uploaded images using Event Grid

Azure 事件网格是一项用于云的事件处理服务,Azure Event Grid is an eventing service for the cloud. 可使你创建由 Azure 服务或第三方资源引发的事件的订阅。Event Grid enables you to create subscriptions to events raised by Azure services or third-party resources.

本教程是存储教程系列中的第二部分。This tutorial is part two of a series of Storage tutorials. 它对前面存储教程的内容进行了扩充,以学习如何使用 Azure 事件网格和 Azure Functions 添加无服务器自动缩略图生成。It extends the previous Storage tutorial to add serverless automatic thumbnail generation using Azure Event Grid and Azure Functions. 事件网格可使 Azure Functions 响应 Azure Blob 存储事件,并生成上传图像的缩略图。Event Grid enables Azure Functions to respond to Azure Blob storage events and generate thumbnails of uploaded images. 针对 Blob 存储创建事件会创建一个事件订阅。An event subscription is created against the Blob storage create event. 当将 blob 添加到特定 Blob 存储容器时,将调用一个函数终结点。When a blob is added to a specific Blob storage container, a function endpoint is called. 从事件网格传递到函数绑定的数据用于访问 blob 并生成缩略图。Data passed to the function binding from Event Grid is used to access the blob and generate the thumbnail image.

可以使用 Azure CLI 和 Azure 门户将调整大小功能添加到现有图像上传应用。You use the Azure CLI and the Azure portal to add the resizing functionality to an existing image upload app.

本教程介绍如何执行下列操作:In this tutorial, you learn how to:

  • 创建常规 Azure 存储帐户Create a general Azure Storage account
  • 使用 Azure Functions 部署无服务器代码Deploy serverless code using Azure Functions
  • 在事件网格中创建 Blob 存储事件订阅Create a Blob storage event subscription in Event Grid

先决条件Prerequisites

备注

本文进行了更新,以便使用新的 Azure PowerShell Az 模块。This article has been updated to use the new Azure PowerShell Az module. 你仍然可以使用 AzureRM 模块,至少在 2020 年 12 月之前,它将继续接收 bug 修补程序。You can still use the AzureRM module, which will continue to receive bug fixes until at least December 2020. 若要详细了解新的 Az 模块和 AzureRM 兼容性,请参阅新 Azure Powershell Az 模块简介To learn more about the new Az module and AzureRM compatibility, see Introducing the new Azure PowerShell Az module. 有关 Az 模块安装说明,请参阅安装 Azure PowerShellFor Az module installation instructions, see Install Azure PowerShell.

完成本教程:To complete this tutorial:

必须已完成以前的 Blob 存储教程:使用 Azure 存储在云中上传图像数据You must have completed the previous Blob storage tutorial: Upload image data in the cloud with Azure Storage.

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

如果之前未在订阅中注册事件网格资源提供程序,请确保已注册。If you've not previously registered the Event Grid resource provider in your subscription, make sure it's registered.

az provider register --namespace Microsoft.EventGrid

使用 Azure Cloud ShellUse Azure Cloud Shell

Azure 托管 Azure Cloud Shell(一个可通过浏览器使用的交互式 shell 环境)。Azure hosts Azure Cloud Shell, an interactive shell environment that you can use through your browser. 通过 Cloud Shell 可以将 bashPowerShell 与 Azure 服务配合使用。Cloud Shell lets you use either bash or PowerShell to work with Azure services. 可以使用 Azure Cloud Shell 预安装的命令来运行本文中的代码,而不必在本地环境中安装任何内容。You can use the Cloud Shell pre-installed commands to run the code in this article without having to install anything on your local environment.

若要启动 Azure Cloud Shell,请执行以下操作:To launch Azure Cloud Shell:

选项Option 示例/链接Example/Link
选择代码块右上角的“试用”。 Select Try It in the upper-right corner of a code block. 选择“试用” 不会自动将代码复制到 Cloud Shell。Selecting Try It doesn't automatically copy the code to Cloud Shell. Azure Cloud Shell 的“试用”示例
转到 https://shell.azure.com 或选择“启动 Cloud Shell” 按钮可在浏览器中打开 Cloud Shell。Go to https://shell.azure.com or select the Launch Cloud Shell button to open Cloud Shell in your browser. <a href="https://shell.azure.com" title="启动 Azure Cloud Shell
选择 Azure 门户右上方菜单栏中的“Cloud Shell” 按钮。Select the Cloud Shell button on the top-right menu bar in the Azure portal. Azure 门户中的“Cloud Shell”按钮

若要在 Azure Cloud Shell 中运行本文中的代码,请执行以下操作:To run the code in this article in Azure Cloud Shell:

  1. 启动 Cloud Shell。Launch Cloud Shell.
  2. 选择代码块上的“复制”按钮 以复制代码。Select the Copy button on a code block to copy the code.
  3. 在 Windows 和 Linux 上使用 Ctrl+Shift+V 将代码粘贴到 Cloud Shell 会话中,或在 macOS 上使用 Cmd+Shift+V 将代码粘贴到 Cloud Shell 会话中。Paste the code into the Cloud Shell session with Ctrl+Shift+V on Windows and Linux, or Cmd+Shift+V on macOS.
  4. Enter 运行此代码。Press Enter to run the code.

如果选择在本地安装并使用 CLI,本教程要求使用 Azure CLI 2.0.14 或更高版本。If you choose to install and use the CLI locally, this tutorial requires the Azure CLI version 2.0.14 or later. 运行 az --version 即可查找版本。Run az --version to find the version. 如果需要进行安装或升级,请参阅安装 Azure CLIIf you need to install or upgrade, see Install Azure CLI.

如果不使用 Cloud Shell,必须先使用 az login 进行登录。If you are not using Cloud Shell, you must first sign in using az login.

创建 Azure 存储帐户Create an Azure Storage account

Azure Functions 需要一个常规存储帐户。Azure Functions requires a general storage account. 除了在上一教程中创建的 Blob 存储帐户,另请使用 az storage account create 命令在资源组中创建一个常规的独立存储帐户。In addition to the Blob storage account you created in the previous tutorial, create a separate general storage account in the resource group by using the az storage account create command. 存储帐户名称必须为 3 到 24 个字符,并且只能包含数字和小写字母。Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only.

  1. 设置一个变量,用于存储在上一教程中创建的资源组的名称。Set a variable to hold the name of the resource group that you created in the previous tutorial.

    resourceGroupName=myResourceGroup
    
  2. 为 Azure Functions 所需的新存储帐户的名称设置一个变量。Set a variable for the name of the new storage account that Azure Functions requires.

    functionstorage=<name of the storage account to be used by the function>
    
  3. 为 Azure 函数创建存储帐户。Create the storage account for the Azure function.

    az storage account create --name $functionstorage --location southeastasia \
    --resource-group $resourceGroupName --sku Standard_LRS --kind storage
    

创建函数应用Create a function app

必须使用 Function App 托管函数的执行。You must have a function app to host the execution of your function. Function App 提供一个环境,以便在不使用服务器的情况下执行函数代码。The function app provides an environment for serverless execution of your function code. 使用 az functionapp create 命令创建 Function App。Create a function app by using the az functionapp create command.

在以下命令中,请提供你自己的唯一的函数应用名称。In the following command, provide your own unique function app name. 函数应用名称用作该函数应用的默认 DNS 域,因此,该名称需要在 Azure 的所有应用中保持唯一。The function app name is used as the default DNS domain for the function app, and so the name needs to be unique across all apps in Azure.

  1. 为将要创建的函数应用指定一个名称。Specify a name for the function app that's to be created.

    functionapp=<name of the function app>
    
  2. 创建 Azure 函数。Create the Azure function.

    az functionapp create --name $functionapp --storage-account $functionstorage \
    --resource-group $resourceGroupName --consumption-plan-location southeastasia
    

现在,必须对函数应用进行配置,以便连接到在以前的教程中创建的 Blob 存储帐户。Now you must configure the function app to connect to the Blob storage account you created in the previous tutorial.

配置函数应用Configure the function app

该函数需要 Blob 存储帐户的凭据,这些凭据是使用 az functionapp config appsettings set 命令添加到函数应用的应用程序设置的。The function needs credentials for the Blob storage account, which are added to the application settings of the function app using the az functionapp config appsettings set command.

blobStorageAccount=<name of the Blob storage account you created in the previous tutorial>
storageConnectionString=$(az storage account show-connection-string --resource-group $resourceGroupName \
--name $blobStorageAccount --query connectionString --output tsv)

az functionapp config appsettings set --name $functionapp --resource-group $resourceGroupName \
--settings AzureWebJobsStorage=$storageConnectionString THUMBNAIL_CONTAINER_NAME=thumbnails \
THUMBNAIL_WIDTH=100 FUNCTIONS_EXTENSION_VERSION=~2

FUNCTIONS_EXTENSION_VERSION=~2 设置使函数应用在 Azure Functions 运行时的 2.x 版上运行。The FUNCTIONS_EXTENSION_VERSION=~2 setting makes the function app run on version 2.x of the Azure Functions runtime.

现在可以将函数代码项目部署到此函数应用。You can now deploy a function code project to this function app.

部署函数代码Deploy the function code

GitHub 上提供示例 C# 重设大小函数。The sample C# resize function is available on GitHub. 使用 az functionapp deployment source config 命令将此代码项目部署到函数应用。Deploy this code project to the function app by using the az functionapp deployment source config command.

az functionapp deployment source config --name $functionapp --resource-group $resourceGroupName --branch master --manual-integration --repo-url https://github.com/Azure-Samples/function-image-upload-resize

图像大小调整函数由事件网关服务发送的 HTTP 请求触发。The image resize function is triggered by HTTP requests sent to it from the Event Grid service. 可以通过创建事件订阅来告知事件网格:你需要在函数的 URL 处获取这些通知。You tell Event Grid that you want to get these notifications at your function's URL by creating an event subscription. 在本教程中,你订阅到 Blob 创建的事件。For this tutorial you subscribe to blob-created events.

从事件网格通知传递到函数的数据包括 Blob 的 URL。The data passed to the function from the Event Grid notification includes the URL of the blob. 该 URL 反过来传递到输入绑定,以便从 Blob 存储获取上传的图像。That URL is in turn passed to the input binding to obtain the uploaded image from Blob storage. 该函数生成缩略图,并将生成的流写入 Blob 存储中的单独容器。The function generates a thumbnail image and writes the resulting stream to a separate container in Blob storage.

此项目使用 EventGridTrigger 作为触发器类型。This project uses EventGridTrigger for the trigger type. 建议使用事件网格触发器而不是泛型 HTTP 触发器。Using the Event Grid trigger is recommended over generic HTTP triggers. 事件网格会自动验证事件网格函数触发器。Event Grid automatically validates Event Grid Function triggers. 使用泛型 HTTP 触发器时,必须实现验证响应With generic HTTP triggers, you must implement the validation response.

若要详细了解此函数,请参阅 function.json 和 run.csx 文件To learn more about this function, see the function.json and run.csx files.

函数项目代码直接从公共示例存储库部署。The function project code is deployed directly from the public sample repository. 要了解有关 Azure Functions 部署选项的详细信息,请参阅 Azure Functions 的持续部署To learn more about deployment options for Azure Functions, see Continuous deployment for Azure Functions.

创建事件订阅Create an event subscription

事件订阅指示要发送到特定终结点的提供程序生成的事件。An event subscription indicates which provider-generated events you want sent to a specific endpoint. 在这种情况下,终结点由函数公开。In this case, the endpoint is exposed by your function. 使用以下步骤创建一个事件订阅,以便向 Azure 门户中的函数发送通知:Use the following steps to create an event subscription that sends notifications to your function in the Azure portal:

  1. Azure 门户的左侧菜单上选择“所有服务”,然后选择“Function App”。In the Azure portal, select All Services on the left menu, and then select Function Apps.

    在 Azure 门户中浏览到 Function App

  2. 展开函数应用,选择 Thumbnail 函数,然后选择“添加事件网格订阅”。Expand your function app, choose the Thumbnail function, and then select Add Event Grid subscription.

    在 Azure 门户中浏览到 Function App

  3. 使用表中指定的事件订阅设置。Use the event subscription settings as specified in the table.

    基于 Azure 门户中的函数创建事件订阅

    设置Setting 建议的值Suggested value 说明Description
    名称Name imageresizersubimageresizersub 标识新事件订阅的名称。Name that identifies your new event subscription.
    主题类型Topic type 存储帐户Storage accounts 选择存储帐户事件提供程序。Choose the Storage account event provider.
    订阅Subscription Azure 订阅Your Azure subscription 默认情况下,选择当前的 Azure 订阅。By default, your current Azure subscription is selected.
    资源组Resource group myResourceGroupmyResourceGroup 选择“使用现有”,然后选择此教程中使用的资源组。Select Use existing and choose the resource group you have been using in this tutorial.
    资源Resource 你的 Blob 存储帐户Your Blob storage account 使用你创建 Blob 存储帐户。Choose the Blob storage account you created.
    事件类型Event types 已创建 blobBlob created 除“已创建 Blob”以外,取消选中所有其他类型。Uncheck all types other than Blob created. 只有 Microsoft.Storage.BlobCreated 的事件类型传递给函数。Only event types of Microsoft.Storage.BlobCreated are passed to the function.
    订阅者类型Subscriber type 自动生成autogenerated 预定义为 Webhook。Pre-defined as Web Hook.
    订阅者终结点Subscriber endpoint 自动生成autogenerated 使用为你生成的终结点 URL。Use the endpoint URL that is generated for you.
  4. 切换到“Filter”选项卡,然后执行以下操作:Switch to the Filter tab, and do the following actions:

    1. 选择“启用主题筛选”选项。Select Enable subject filtering option.

    2. 对于“主题开头为”,输入以下值:/blobServices/default/containers/images/blobs/For Subject begins with, enter the following value : /blobServices/default/containers/images/blobs/.

      指定事件订阅筛选器

  5. 选择“创建”以添加事件订阅。Select Create to add the event subscription. 这将创建一个事件订阅,在将 Blob 添加到 images 容器时,该订阅会触发 Thumbnail 函数。This creates an event subscription that triggers Thumbnail function when a blob is added to the images container. 此函数重设图像大小,然后将图像添加到 thumbnails 容器。The function resizes the images and adds them to the thumbnails container.

至此,已配置后端服务,可在示例 Web 应用中测试调整图像大小功能。Now that the backend services are configured, you test the image resize functionality in the sample web app.

测试示例应用Test the sample app

要在 Web 应用中测试调整图像大小功能,请浏览到已发布应用的 URL。To test image resizing in the web app, browse to the URL of your published app. Web 应用的默认 URL 为 https://<web_app>.azurewebsites.netThe default URL of the web app is https://<web_app>.azurewebsites.net.

单击“上传照片”区域,选择并上传文件。Click the Upload photos region to select and upload a file. 或者,也可以将照片拖动到此区域。You can also drag a photo to this region.

注意,上传的图像消失后,上传图像的副本将显示在生成的缩略图轮廓中。Notice that after the uploaded image disappears, a copy of the uploaded image is displayed in the Generated thumbnails carousel. 此图像在通过函数重设大小后会被添加到 thumbnails 容器中,再由 Web 客户端下载。This image was resized by the function, added to the thumbnails container, and downloaded by the web client.

在浏览器中发布的 Web 应用