使用 Functions 将消息添加到 Azure 存储队列Add messages to an Azure Storage queue using Functions

在 Azure Functions 中,可以通过输入和输出绑定以声明方式将外部服务中的数据提供给代码使用。In Azure Functions, input and output bindings provide a declarative way to make data from external services available to your code. 在本快速入门中,可以使用输出绑定,这样便可以当某个 HTTP 请求触发某个函数时,在队列中创建一条消息。In this quickstart, you use an output binding to create a message in a queue when a function is triggered by an HTTP request. 可以使用 Azure 存储资源管理器查看函数创建的队列消息:You use Azure Storage Explorer to view the queue messages that your function creates:

存储资源管理器中显示的队列消息

先决条件Prerequisites

若要完成本快速入门教程,需先执行以下操作:To complete this quickstart:

添加输出绑定Add an output binding

在此部分,请使用门户 UI 将队列存储输出绑定添加到此前创建的函数。In this section, you use the portal UI to add a queue storage output binding to the function you created earlier. 有了此绑定,就可以在尽量减少代码编写工作的情况下在队列中创建消息。This binding will make it possible to write minimal code to create a message in a queue. 不需为打开存储连接、创建队列、获取队列引用之类的任务编写代码。You don't have to write code for tasks such as opening a storage connection, creating a queue, or getting a reference to a queue. Azure Functions 运行时和队列输出绑定为你处理这些任务。The Azure Functions runtime and queue output binding take care of those tasks for you.

  1. 在 Azure 门户中,打开在通过 Azure 门户创建第一个函数中创建的函数应用的函数应用页。In the Azure portal, open the function app page for the function app that you created in Create your first function from the Azure portal. 为此,请选择“所有服务”>“Function App”,然后选择你的函数应用。To do this, select All services > Function Apps, and then select your function app.

  2. 选择在此前的那个快速入门中创建的函数。Select the function that you created in that earlier quickstart.

  3. 选择“集成”>“新建输出”>“Azure 队列存储”。Select Integrate > New output > Azure Queue storage.

  4. 单击“选择”。Click Select.

    将队列存储输出绑定添加到 Azure 门户中的函数。

  5. 如果收到“扩展未安装”消息,请选择“安装”以在函数应用中安装存储绑定扩展。If you get an Extensions not installed message, choose Install to install the Storage bindings extension in the function app. 这可能需要一到两分钟。This may take a minute or two.

    安装存储绑定扩展

  6. 在“Azure 队列存储输出”下,使用在此屏幕截图下的表中指定的设置:Under Azure Queue Storage output, use the settings as specified in the table that follows this screenshot:

    将队列存储输出绑定添加到 Azure 门户中的函数。

    设置Setting 建议的值Suggested value 说明Description
    消息参数名称Message parameter name outputQueueItemoutputQueueItem 输出绑定参数的名称。The name of the output binding parameter.
    存储帐户连接Storage account connection AzureWebJobsStorageAzureWebJobsStorage 可以使用 Function App 已在使用的存储帐户连接,也可以创建一个新的存储帐户连接。You can use the storage account connection already being used by your function app, or create a new one.
    队列名称Queue name outqueueoutqueue 要连接到存储帐户中的队列的名称。The name of the queue to connect to in your Storage account.
  7. 单击“保存”添加绑定。Click Save to add the binding.

现在,已定义输出绑定,需要更新代码以使用绑定将消息添加到队列。Now that you have an output binding defined, you need to update the code to use the binding to add messages to a queue.

添加使用输出绑定的代码Add code that uses the output binding

在此部分,请添加将消息写入输出队列的代码。In this section, you add code that writes a message to the output queue. 该消息包括在查询字符串中传递到 HTTP 触发器的值。The message includes the value that is passed to the HTTP trigger in the query string. 例如,如果查询字符串包含 name=Azure,则队列消息将是“传递给函数的名称:Azure”。For example, if the query string includes name=Azure, the queue message will be Name passed to the function: Azure.

  1. 选择函数以在编辑器中显示函数代码。Select your function to display the function code in the editor.

  2. 根据函数语言更新函数代码:Update the function code depending on your function language:

    向方法签名添加 outputQueueItem 参数,如以下示例所示。Add an outputQueueItem parameter to the method signature as shown in the following example.

    public static async Task<IActionResult> Run(HttpRequest req,
        ICollector<string> outputQueueItem, ILogger log)
    {
        ...
    }
    

    return 语句之前的函数体中,添加使用参数创建队列消息的代码。In the body of the function just before the return statement, add code that uses the parameter to create a queue message.

    outputQueueItem.Add("Name passed to the function: " + name);
    
  3. 选择“保存”以保存更改。Select Save to save changes.

测试函数Test the function

  1. 保存代码更改后,选择“运行”。After the code changes are saved, select Run.

    将队列存储输出绑定添加到 Azure 门户中的函数。

    请注意,请求正文包含 nameAzureNotice that the Request body contains the name value Azure. 此值显示在队列消息中,该消息是在调用函数时创建的。This value appears in the queue message that is created when the function is invoked.

    如果不想选择此处的“运行”,也可调用该函数,方法是在浏览器中输入 URL,然后在查询字符串中指定 name 值。As an alternative to selecting Run here, you can call the function by entering a URL in a browser and specifying the name value in the query string. 此浏览器方法在以前的快速入门中演示过。The browser method is shown in the previous quickstart.

  2. 检查日志以确保该函数成功。Check the logs to make sure that the function succeeded.

首次使用输出绑定时,Functions 运行时会在存储帐户中创建名为 outqueue 的新队列。A new queue named outqueue is created in your Storage account by the Functions runtime when the output binding is first used. 可以使用存储资源管理器来验证是否已创建队列以及其中的消息。You'll use Storage Explorer to verify that the queue and a message in it were created.

将存储资源管理器连接到帐户Connect Storage Explorer to your account

如果已安装存储资源管理器并将其连接到此快速入门中使用的存储帐户,则请跳过此部分。Skip this section if you have already installed Storage Explorer and connected it to the storage account that you're using with this quickstart.

  1. 运行 Azure 存储资源管理器工具,选择左侧的“连接”图标,选择“使用存储帐户名称和密钥”,然后选择“下一步”。Run the Azure Storage Explorer tool, select the connect icon on the left, choose Use a storage account name and key, and select Next.

    运行“存储帐户资源管理器”工具。

  2. 在 Azure 门户的函数应用页中选择函数,然后选择“集成”。In the Azure portal, on the function app page, select your function and then select Integrate.

  3. 选择在前面的步骤中添加的“Azure 队列存储”输出绑定。Select the Azure Queue storage output binding that you added in an earlier step.

  4. 展开页面底部的“文档”部分。Expand the Documentation section at the bottom of the page.

    门户显示的凭据可以在存储资源管理器中用于连接到存储帐户。The portal shows credentials that you can use in Storage Explorer to connect to the storage account.

    获取存储帐户连接凭据。

  5. 从门户复制“帐户名称”值,将其粘贴在存储资源管理器的“帐户名称”框中。Copy the Account Name value from the portal and paste it in the Account name box in Storage Explorer.

  6. 单击“帐户密钥”旁边的显示/隐藏图标以显示该值,然后复制“帐户密钥”值并将其粘贴在存储资源管理器的“帐户密钥”框中。Click the show/hide icon next to Account Key to display the value, and then copy the Account Key value and paste it in the Account key box in Storage Explorer.

  7. 选择“下一步”>“连接”。Select Next > Connect.

    粘贴存储凭据,然后进行连接。

检查输出队列Examine the output queue

  1. 在存储资源管理器中选择用于本快速入门的存储帐户。In Storage Explorer, select the storage account that you're using for this quickstart.

  2. 展开“队列”节点,然后选择名为 outqueue 的队列。Expand the Queues node, and then select the queue named outqueue.

    此队列包含在运行 HTTP 触发的函数时队列输出绑定创建的消息。The queue contains the message that the queue output binding created when you ran the HTTP-triggered function. 如果使用 Azure 的默认 name 值调用了此函数,则队列消息为“传递给函数的名称:Azure”。If you invoked the function with the default name value of Azure, the queue message is Name passed to the function: Azure.

    存储资源管理器中显示的队列消息

  3. 再次运行函数,此时会看到新消息出现在队列中。Run the function again, and you'll see a new message appear in the queue.

清理资源Clean up resources

本教程系列中的其他快速入门教程是在本文的基础上制作的。Other quick starts in this collection build upon this quick start. 如果打算使用后续的快速入门、教程或者在本快速入门中创建的任何服务,请勿清理这些资源。If you plan to work with subsequent quick starts, tutorials, or with any of the services you have created in this quick start, do not clean up the resources.

Azure 中的资源是指函数应用、函数、存储帐户等。Resources in Azure refers to function apps, functions, storage accounts, and so forth. 这些资源可以组合到资源组中,删除该组即可删除组中的所有内容。They are grouped into resource groups, and you can delete everything in a group by deleting the group.

已创建完成这些快速入门所需的资源。You created resources to complete these quickstarts. 这些资源可能需要付费,具体取决于帐户状态服务定价You may be billed for these resources, depending on your account status and service pricing. 如果不再需要这些资源,请参阅下面介绍的资源删除方法:If you don't need the resources anymore, here's how to delete them:

  1. 在 Azure 门户中转到“资源组”页。In the Azure portal, go to the Resource group page.

    若要从函数应用页转到该页,请选择“概览”选项卡,然后选择“资源组”下的链接。To get to that page from the function app page, select the Overview tab and then select the link under Resource group.

    从函数应用页选择要删除的资源组。

    若要从仪表板转到该页,请选择“资源组”,然后选择用于本快速入门的资源组。To get to that page from the dashboard, select Resource groups, and then select the resource group that you used for this quickstart.

  2. 在“资源组”页中查看所包括的资源的列表,然后验证这些资源是否是要删除的。In the Resource group page, review the list of included resources, and verify that they are the ones you want to delete.

  3. 选择“删除资源组”,然后按说明操作。Select Delete resource group, and follow the instructions.

    可能需要数分钟才能删除完毕。Deletion may take a couple of minutes. 完成后会显示一个通知,持续数秒。When it's done, a notification appears for a few seconds. 也可以选择页面顶部的钟形图标来查看通知。You can also select the bell icon at the top of the page to view the notification.

后续步骤Next steps

本快速入门介绍了如何将输出绑定添加到现有函数。In this quickstart, you added an output binding to an existing function. 有关队列存储绑定的详细信息,请参阅 Azure Functions 存储队列绑定For more information about binding to Queue storage, see Azure Functions Storage queue bindings.