使用 Visual Studio Code 将 Azure Functions 连接到 Azure 存储Connect Azure Functions to Azure Storage using Visual Studio Code

无需编写自己的集成代码,即可使用 Azure Functions 将 Azure 服务和其他资源连接到函数。Azure Functions lets you connect Azure services and other resources to functions without having to write your own integration code. 这些绑定表示输入和输出,在函数定义中声明。 These bindings, which represent both input and output, are declared within the function definition. 绑定中的数据作为参数提供给函数。Data from bindings is provided to the function as parameters. 触发器是一种特殊类型的输入绑定。 A trigger is a special type of input binding. 尽管一个函数只有一个触发器,但它可以有多个输入和输出绑定。Although a function has only one trigger, it can have multiple input and output bindings. 有关详细信息,请参阅 Azure Functions 触发器和绑定的概念To learn more, see Azure Functions triggers and bindings concepts.

本文介绍如何使用 Visual Studio Code 将在前一篇快速入门文章中创建的函数连接到 Azure 存储。This article shows you how to use Visual Studio Code to connect the function you created in the previous quickstart article to Azure Storage. 添加到此函数的输出绑定会将 HTTP 请求中的数据写入到 Azure 队列存储队列中的消息。The output binding that you add to this function writes data from the HTTP request to a message in an Azure Queue storage queue.

大多数绑定都需要一个存储的连接字符串,函数将使用该字符串来访问绑定的服务。Most bindings require a stored connection string that Functions uses to access the bound service. 为便于操作,请使用连同函数应用一起创建的存储帐户。To make it easier, you use the Storage account that you created with your function app. 与此帐户建立的连接已存储在名为 AzureWebJobsStorage 的应用设置中。The connection to this account is already stored in an app setting named AzureWebJobsStorage.

配置本地环境Configure your local environment

在开始本文之前,必须满足以下要求:Before you start this article, you must meet the following requirements:

本文假设你已从 Visual Studio Code 登录到 Azure 订阅。This article assumes that you're already signed in to your Azure subscription from Visual Studio Code. 你可以通过从命令面板运行 Azure: Sign In 进行登录。You can sign in by running Azure: Sign In from the command palette.

下载函数应用设置Download the function app settings

前一篇快速入门文章中,你已在 Azure 中创建了一个函数应用,并创建了所需的存储帐户。In the previous quickstart article, you created a function app in Azure along with the required Storage account. 此帐户的连接字符串安全存储在 Azure 中的应用设置内。The connection string for this account is stored securely in app settings in Azure. 在本文中,你要将消息写入到同一帐户中的存储队列。In this article, you write messages to a Storage queue in the same account. 若要在本地运行函数时连接到该存储帐户,必须将应用设置下载到 local.settings.json 文件。To connect to your Storage account when running the function locally, you must download app settings to the local.settings.json file.

  1. 按 F1 键打开命令面板,然后搜索并运行命令 Azure Functions: Download Remote Settings....Press the F1 key to open the command palette, then search for and run the command Azure Functions: Download Remote Settings.....

  2. 选择你在前一篇文章中创建的函数应用。Choose the function app you created in the previous article. 选择“全是”覆盖现有本地设置。Select Yes to all to overwrite the existing local settings.

    重要

    由于 local.settings.json 文件包含机密,因此请勿发布,应将其从源代码管理中排除。Because it contains secrets, the local.settings.json file never gets published, and is excluded from source control.

  3. 复制值 AzureWebJobsStorage,这是存储帐户连接字符串值的键。Copy the value AzureWebJobsStorage, which is the key for the Storage account connection string value. 你将使用此连接来验证输出绑定是否按预期方式工作。You use this connection to verify that the output binding works as expected.

注册绑定扩展Register binding extensions

由于你使用队列存储输出绑定,因此在运行项目之前,必须安装存储绑定扩展。Because you're using a Queue storage output binding, you must have the Storage bindings extension installed before you run the project.

项目已配置为使用扩展捆绑包,因此会自动安装一组预定义的扩展包。Your project has been configured to use extension bundles, which automatically installs a predefined set of extension packages.

在项目根目录下的 host.json 文件中启用扩展捆绑包,如下所示:Extension bundles usage is enabled in the host.json file at the root of the project, which appears as follows:

{
  "version": "2.0",
  "extensionBundle": {
    "id": "Microsoft.Azure.Functions.ExtensionBundle",
    "version": "[1.*, 2.0.0)"
  } 
}

除了 HTTP 和计时器触发器,绑定将实现为扩展包。With the exception of HTTP and timer triggers, bindings are implemented as extension packages. 在终端窗口中运行以下 dotnet add package 命令,将存储扩展包添加到项目中。Run the following dotnet add package command in the Terminal window to add the Storage extension package to your project.

dotnet add package Microsoft.Azure.WebJobs.Extensions.Storage

现在,你可以将存储输出绑定添加到项目。Now, you can add the storage output binding to your project.

添加输出绑定Add an output binding

在 Functions 中,每种类型的绑定都需要一个 directiontype,以及要在 function.json 文件中定义的唯一 nameIn Functions, each type of binding requires a direction, type, and a unique name to be defined in the function.json file. 定义这些属性的方式取决于函数应用的语言。The way you define these attributes depends on the language of your function app.

直接在 function.json 文件中定义绑定属性。Binding attributes are defined directly in the function.json file. 根据绑定类型,可能还需要其他属性。Depending on the binding type, additional properties may be required. 队列输出配置描述 Azure 存储队列绑定所需的字段。The queue output configuration describes the fields required for an Azure Storage queue binding. 扩展可以轻松地将绑定添加到 function.json 文件。The extension makes it easy to add bindings to the function.json file.

要创建绑定,请右键单击(在 macOS 系统上,请按住 Ctrl 并单击)HttpTrigger 文件夹中的 function.json 文件,并选择“添加绑定...” 。请按照提示为新绑定定义以下绑定属性:To create a binding, right-click (Ctrl+click on macOS) the function.json file in your HttpTrigger folder and choose Add binding.... Follow the prompts to define the following binding properties for the new binding:

PromptPrompt ValueValue 说明Description
选择绑定方向Select binding direction out 该绑定是输出绑定。The binding is an output binding.
选择具有方向的绑定...Select binding with direction... Azure Queue Storage 该绑定是 Azure 存储队列绑定。The binding is an Azure Storage queue binding.
用于在代码中标识此绑定的名称The name used to identify this binding in your code msg 用于标识代码中引用的绑定参数的名称。Name that identifies the binding parameter referenced in your code.
要将消息发送到的队列The queue to which the message will be sent outqueue 绑定要写入到的队列的名称。The name of the queue that the binding writes to. 如果 queueName 不存在,首次使用绑定时,它会创建该属性。When the queueName doesn't exist, the binding creates it on first use.
从 local.setting.json 中选择设置Select setting from "local.setting.json" AzureWebJobsStorage 包含存储帐户连接字符串的应用程序设置的名称。The name of an application setting that contains the connection string for the Storage account. AzureWebJobsStorage 设置包含连同函数应用一起创建的存储帐户的连接字符串。The AzureWebJobsStorage setting contains the connection string for the Storage account you created with the function app.

绑定将添加到 function.json 中的 bindings 数组,应如下所示:A binding is added to the bindings array in your function.json, which should look like the following:

{
 "type": "queue",
 "direction": "out",
 "name": "msg",
 "queueName": "outqueue",
 "connection": "AzureWebJobsStorage"
}

在 C# 类库项目中,绑定被定义为函数方法上的绑定属性。In a C# class library project, the bindings are defined as binding attributes on the function method. 然后,基于这些属性自动生成 Functions 所需的 function.json 文件。The function.json file required by Functions is then auto-generated based on these attributes.

打开 HttpExample.cs 项目文件,并将以下参数添加到 Run 方法定义中:Open the HttpExample.cs project file and add the following parameter to the Run method definition:

[Queue("outqueue"),StorageAccount("AzureWebJobsStorage")] ICollector<string> msg,

msg 参数为 ICollector<T> 类型,表示函数完成时写入输出绑定的消息集合。The msg parameter is an ICollector<T> type, which represents a collection of messages that are written to an output binding when the function completes. 在这种情况下,输出是名为的 outqueue 存储队列。In this case, the output is a storage queue named outqueue. 存储帐户的连接字符串由 StorageAccountAttribute 设置。The connection string for the Storage account is set by the StorageAccountAttribute. 此属性指示包含存储帐户连接字符串的设置,可以在类、方法或参数级别应用。This attribute indicates the setting that contains the Storage account connection string and can be applied at the class, method, or parameter level. 在这种情况下,可以省略 StorageAccountAttribute,因为你已使用默认存储帐户。In this case, you could omit StorageAccountAttribute because you are already using the default storage account.

Run 方法定义如下所示:The Run method definition should now look like the following:

[FunctionName("HttpExample")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, 
    [Queue("outqueue"),StorageAccount("AzureWebJobsStorage")] ICollector<string> msg, 
    ILogger log)

在 Java 项目中,绑定被定义为函数方法上的绑定注释。In a Java project, the bindings are defined as binding annotations on the function method. 然后根据这些注释自动生成 function.json 文件。The function.json file is then autogenerated based on these annotations.

浏览到函数代码在 src/main/java 下的位置,打开 Function.java 项目文件,然后将以下参数添加到 run 方法定义:Browse to the location of your function code under src/main/java, open the Function.java project file, and add the following parameter to the run method definition:

@QueueOutput(name = "msg", queueName = "outqueue", 
connection = "AzureWebJobsStorage") OutputBinding<String> msg,

msg 参数为 OutputBinding<T> 类型,表示函数完成时作为消息写入到输出绑定的字符串集合。The msg parameter is an OutputBinding<T> type, which represents a collection of strings that are written as messages to an output binding when the function completes. 在这种情况下,输出是名为的 outqueue 存储队列。In this case, the output is a storage queue named outqueue. 存储帐户的连接字符串由 connection 方法设置。The connection string for the Storage account is set by the connection method. 请传递包含存储帐户连接字符串的应用程序设置,而不是传递连接字符串本身。Rather than the connection string itself, you pass the application setting that contains the Storage account connection string.

run 方法定义现在应如以下示例所示:The run method definition should now look like the following example:

@FunctionName("HttpExample")
public HttpResponseMessage run(
        @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) 
        HttpRequestMessage<Optional<String>> request, 
        @QueueOutput(name = "msg", queueName = "outqueue", 
        connection = "AzureWebJobsStorage") OutputBinding<String> msg, 
        final ExecutionContext context) {

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

定义绑定后,可以使用绑定的 name,将其作为函数签名中的属性进行访问。After the binding is defined, you can use the name of the binding to access it as an attribute in the function signature. 使用输出绑定时,无需使用 Azure 存储 SDK 代码进行身份验证、获取队列引用或写入数据。By using an output binding, you don't have to use the Azure Storage SDK code for authentication, getting a queue reference, or writing data. Functions 运行时和队列输出绑定将为你执行这些任务。The Functions runtime and queue output binding do those tasks for you.

添加在 context.bindings 上使用 msg 输出绑定对象来创建队列消息的代码。Add code that uses the msg output binding object on context.bindings to create a queue message. 请在 context.res 语句之前添加此代码。Add this code before the context.res statement.

// Add a message to the Storage queue.
context.bindings.msg = "Name passed to the function: " + 
(req.query.name || req.body.name);

此时,你的函数应如下所示:At this point, your function should look as follows:

module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    if (req.query.name || (req.body && req.body.name)) {
        // Add a message to the Storage queue.
        context.bindings.msg = "Name passed to the function: " + 
        (req.query.name || req.body.name);
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

添加在 context.bindings 上使用 msg 输出绑定对象来创建队列消息的代码。Add code that uses the msg output binding object on context.bindings to create a queue message. 请在 context.res 语句之前添加此代码。Add this code before the context.res statement.

context.bindings.msg = name;

此时,你的函数应如下所示:At this point, your function should look as follows:

import { AzureFunction, Context, HttpRequest } from "@azure/functions"

const httpTrigger: AzureFunction = async function (context: Context, req: HttpRequest): Promise<void> {
    context.log('HTTP trigger function processed a request.');
    const name = (req.query.name || (req.body && req.body.name));

    if (name) {
        // Add a message to the storage queue, 
        // which is the name passed to the function.
        context.bindings.msg = name; 
        // Send a "hello" response.
        context.res = {
            // status: 200, /* Defaults to 200 */
            body: "Hello " + (req.query.name || req.body.name)
        };
    }
    else {
        context.res = {
            status: 400,
            body: "Please pass a name on the query string or in the request body"
        };
    }
};

export default httpTrigger;

添加使用 Push-OutputBinding cmdlet 通过 msg 输出绑定将文本写入队列的代码。Add code that uses the Push-OutputBinding cmdlet to write text to the queue using the msg output binding. if 语句中设置“正常”状态之前,请添加此代码。Add this code before you set the OK status in the if statement.

$outputMsg = $name
Push-OutputBinding -name msg -Value $outputMsg

此时,你的函数应如下所示:At this point, your function should look as follows:

using namespace System.Net

# Input bindings are passed in via param block.
param($Request, $TriggerMetadata)

# Write to the Azure Functions log stream.
Write-Host "PowerShell HTTP trigger function processed a request."

# Interact with query parameters or the body of the request.
$name = $Request.Query.Name
if (-not $name) {
    $name = $Request.Body.Name
}

if ($name) {
    # Write the $name value to the queue, 
    # which is the name passed to the function.
    $outputMsg = $name
    Push-OutputBinding -name msg -Value $outputMsg

    $status = [HttpStatusCode]::OK
    $body = "Hello $name"
}
else {
    $status = [HttpStatusCode]::BadRequest
    $body = "Please pass a name on the query string or in the request body."
}

# Associate values to output bindings by calling 'Push-OutputBinding'.
Push-OutputBinding -Name Response -Value ([HttpResponseContext]@{
    StatusCode = $status
    Body = $body
})

添加使用 msg 输出绑定对象来创建队列消息的代码。Add code that uses the msg output binding object to create a queue message. 请在方法返回之前添加此代码。Add this code before the method returns.

if (!string.IsNullOrEmpty(name))
{
    // Add a message to the output collection.
    msg.Add(string.Format("Name passed to the function: {0}", name));
}

此时,你的函数应如下所示:At this point, your function should look as follows:

[FunctionName("HttpExample")]
public static async Task<IActionResult> Run(
    [HttpTrigger(AuthorizationLevel.Function, "get", "post", Route = null)] HttpRequest req, 
    [Queue("outqueue"),StorageAccount("AzureWebJobsStorage")] ICollector<string> msg, 
    ILogger log)
{
    log.LogInformation("C# HTTP trigger function processed a request.");

    string name = req.Query["name"];

    string requestBody = await new StreamReader(req.Body).ReadToEndAsync();
    dynamic data = JsonConvert.DeserializeObject(requestBody);
    name = name ?? data?.name;

    if (!string.IsNullOrEmpty(name))
    {
        // Add a message to the output collection.
        msg.Add(string.Format("Name passed to the function: {0}", name));
    }
    return name != null
        ? (ActionResult)new OkObjectResult($"Hello, {name}")
        : new BadRequestObjectResult("Please pass a name on the query string or in the request body");
}

现在可以使用新的 msg 参数,从函数代码写入到输出绑定。Now, you can use the new msg parameter to write to the output binding from your function code. 将以下代码行添加到成功响应之前,以便将 name 的值添加到 msg 输出绑定。Add the following line of code before the success response to add the value of name to the msg output binding.

// Write the name to the message queue.

使用输出绑定时,无需使用 Azure 存储 SDK 代码进行身份验证、获取队列引用或写入数据。When you use an output binding, you don't have to use the Azure Storage SDK code for authentication, getting a queue reference, or writing data. Functions 运行时和队列输出绑定将为你执行这些任务。The Functions runtime and queue output binding do those tasks for you.

run 方法现在应如以下示例所示:Your run method should now look like the following example:

@FunctionName("HttpExample")
public HttpResponseMessage run(
        @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) 
        HttpRequestMessage<Optional<String>> request, 
        @QueueOutput(name = "msg", queueName = "outqueue", 
        connection = "AzureWebJobsStorage") OutputBinding<String> msg, 
        final ExecutionContext context) {
    context.getLogger().info("Java HTTP trigger processed a request.");

    // Parse query parameter
    String query = request.getQueryParameters().get("name");
    String name = request.getBody().orElse(query);

    if (name == null) {
        return request.createResponseBuilder(HttpStatus.BAD_REQUEST)
        .body("Please pass a name on the query string or in the request body").build();
    } else {
        // Write the name to the message queue. 
        msg.setValue(name);

        return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build();
    }

更新测试Update the tests

由于原型还创建一组测试,因此需更新这些测试,以便处理 run 方法签名中的新 msg 参数。Because the archetype also creates a set of tests, you need to update these tests to handle the new msg parameter in the run method signature.

浏览到测试代码在 src/test/java 下的位置,打开 Function.java 项目文件,将 //Invoke 下的代码行替换为以下代码。Browse to the location of your test code under src/test/java, open the Function.java project file, and replace the line of code under //Invoke with the following code.

@SuppressWarnings("unchecked")
final OutputBinding<String> msg = (OutputBinding<String>)mock(OutputBinding.class);
final HttpResponseMessage ret = new Function().run(req, msg, context);

在本地运行函数Run the function locally

Visual Studio Code 与 Azure Functions Core Tools 集成,方便你在将内容发布到 Azure 之前在本地开发计算机上运行此项目。Visual Studio Code integrates with Azure Functions Core Tools to let you run this project on your local development computer before you publish to Azure.

  1. 若要调用函数,请按 F5 启动函数应用项目。To call your function, press F5 to start the function app project. 来自 Core Tools 的输出会显示在“终端” 面板中。Output from Core Tools is displayed in the Terminal panel.

  2. 如果尚未安装 Azure Functions Core Tools,请在提示符下选择“安装”。 If you haven't already installed Azure Functions Core Tools, select Install at the prompt. 安装 Core Tools 后,应用会在“终端” 面板中启动。When the Core Tools are installed, your app starts in the Terminal panel. 可以看到 HTTP 触发函数的 URL 终结点在本地运行。You can see the URL endpoint of your HTTP-triggered function running locally.

    Azure 本地输出

  3. 在 Core Tools 运行时,导航至以下 URL 以执行 GET 请求(其中包括 ?name=Functions 查询字符串)。With Core Tools running, navigate to the following URL to execute a GET request, which includes ?name=Functions query string.

    http://localhost:7071/api/HttpExample?name=Functions

  4. 系统会返回响应,在浏览器中如下所示:A response is returned, which looks like the following in a browser:

    浏览器中的函数 localhost 响应

  5. 有关请求的信息将显示在“终端” 面板中。Information about the request is shown in Terminal panel.

    “终端”面板中的函数执行

  6. 按 Ctrl + C 停止 Core Tools 并断开调试器的连接。Press Ctrl + C to stop Core Tools and disconnect the debugger.

在本地运行函数Run the function locally

Azure Functions Core Tools 与 Visual Studio Code 相集成,可让在本地运行和调试 Azure Functions 项目。Azure Functions Core Tools integrates with Visual Studio Code to let you run and debug an Azure Functions project locally.

  1. 按 F5 启动函数应用项目。Press F5 to start the function app project. 来自 Core Tools 的输出会显示在“终端”面板中。Output from Core Tools is displayed in the Terminal panel.

  2. 在“终端” 面板中,复制 HTTP 触发的函数的 URL 终结点。In the Terminal panel, copy the URL endpoint of your HTTP-triggered function.

    Azure 本地输出

  3. 将查询字符串 ?name=<yourname> 追加到此 URL,然后在另一个 PowerShell 命令提示符下使用 Invoke-RestMethod 执行请求,如下所示:Append the query string ?name=<yourname> to this URL, and then use Invoke-RestMethod in a second PowerShell command prompt to execute the request, as follows:

    PS > Invoke-RestMethod -Method Get -Uri http://localhost:7071/api/HttpTrigger?name=PowerShell
    Hello PowerShell
    

    也可以从浏览器通过以下 URL 执行 GET 请求:You can also execute the GET request from a browser from the following URL:

    http://localhost:7071/api/HttpExample?name=PowerShell

    调用 HttpTrigger 终结点而未将 name 参数作为查询参数或在正文中传递时,该函数将返回 BadRequest 错误。When you call the HttpTrigger endpoint without passing a name parameter either as a query parameter or in the body, the function returns a BadRequest error. 在 run.ps1 中检查代码时,会看到此错误,这是设计使然。When you review the code in run.ps1, you see that this error occurs by design.

  4. 有关请求的信息将显示在“终端” 面板中。Information about the request is shown in Terminal panel.

    “终端”面板中的函数执行

  5. 完成后,按 Ctrl + C 停止 Core Tools。When done, press Ctrl + C to stop Core Tools.

确认该函数可以在本地计算机上正确运行以后,即可将项目发布到 Azure。After you've verified that the function runs correctly on your local computer, it's time to publish the project to Azure.

首次使用输出绑定时,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 was created along with the new message.

更新测试Update the tests

由于原型还创建一组测试,因此需更新这些测试,以便处理 run 方法签名中的新 msg 参数。Because the archetype also creates a set of tests, you need to update these tests to handle the new msg parameter in the run method signature.

浏览到测试代码在 src/test/java 下的位置,打开 Function.java 项目文件,将 //Invoke 下的代码行替换为以下代码。Browse to the location of your test code under src/test/java, open the Function.java project file, and replace the line of code under //Invoke with the following code.

@SuppressWarnings("unchecked")
final OutputBinding<String> msg = (OutputBinding<String>)mock(OutputBinding.class);
final HttpResponseMessage ret = new Function().run(req, msg, context);

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

如果已安装 Azure 存储资源管理器并已将其连接到 Azure 帐户,请跳过此部分。Skip this section if you have already installed Azure Storage Explorer and connected it to your Azure account.

  1. 运行 [Azure存储资源管理器] 工具,选择左侧的“连接”图标,然后选择“添加帐户”。Run the [Azure Storage Explorer] tool, select the connect icon on the left, and select Add an account.

    将 Azure 帐户添加到 Azure 存储资源管理器

  2. 在“连接”对话框中,依次选择“添加 Azure 帐户”、你的“Azure 环境”和“登录...” 。In the Connect dialog, choose Add an Azure account, choose your Azure environment, and select Sign in....

    登录到 Azure 帐户

成功登录到帐户后,将看到与你的帐户关联的所有 Azure 订阅。After you successfully sign in to your account, you see all of the Azure subscriptions associated with your account.

检查输出队列Examine the output queue

  1. 在 Visual Studio Code 中,按 F1 键打开命令面板,然后搜索并运行命令 Azure Storage: Open in Storage Explorer,选择你的存储帐户名称。In Visual Studio Code, press the F1 key to open the command palette, then search for and run the command Azure Storage: Open in Storage Explorer and choose your Storage account name. 随即将在 Azure 存储资源管理器中打开你的存储帐户。Your storage account opens in Azure Storage Explorer.

  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.

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

  3. 再次运行函数,发送另一个请求,此时会看到新消息出现在队列中。Run the function again, send another request, and you'll see a new message appear in the queue.

现在,可将更新的函数应用重新发布到 Azure。Now, it's time to republish the updated function app to Azure.

重新部署并验证更新的应用Redeploy and verify the updated app

  1. 在 Visual Studio Code 中,按 F1 键打开命令面板。In Visual Studio Code, press F1 to open the command palette. 在命令面板中,搜索并选择 Azure Functions: Deploy to function app...In the command palette, search for and select Azure Functions: Deploy to function app....

  2. 选择你在第一篇文章中创建的函数应用。Choose the function app that you created in the first article. 由于你要将项目重新部署到同一个应用,因此请选择“部署”以关闭关于覆盖文件的警告。Because you're redeploying your project to the same app, select Deploy to dismiss the warning about overwriting files.

  3. 部署完成后,可以再次使用 cURL 或浏览器测试重新部署的函数。After deployment completes, you can again use cURL or a browser to test the redeployed function. 与前面一样,请将查询字符串 &name=<yourname> 追加到 URL,如以下示例所示:As before, append the query string &name=<yourname> to the URL, as in the following example:

    curl https://myfunctionapp.chinacloudsites.cn/api/httptrigger?code=cCr8sAxfBiow548FBDLS1....&name=<yourname>
    
  4. 可以查看存储队列中的消息,以再次确认输出绑定是否在队列中生成了新的消息。Again view the message in the storage queue to verify that the output binding again generates a new message in the queue.

清理资源Clean up resources

在 Azure 中,“资源”是指函数应用、函数、存储帐户等。In Azure, resources refer to function apps, functions, storage accounts, and so forth. 这些资源可以组合到资源组中,删除该组即可删除组中的所有内容。They're 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. 在 Visual Studio Code 中,按 F1 键打开命令面板。In Visual Studio Code, press F1 to open the command palette. 在命令面板中,搜索并选择 Azure Functions: Open in portalIn the command palette, search for and select Azure Functions: Open in portal.

  2. 选择你的函数应用,然后按 Enter。Choose your function app, and press Enter. 随即将在 Azure 门户中打开函数应用页面。The function app page opens in the Azure portal.

  3. 在“概览”选项卡中,选择“资源组”旁边的命名链接。In the Overview tab, select the named link next to Resource group.

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

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

  5. 选择“删除资源组”,然后按说明操作。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

现已更新 HTTP 触发的函数,使其将数据写入存储队列。You've updated your HTTP triggered function to write data to a Storage queue. 现在,可以详细了解如何使用 Visual Studio Code 开发 Functions:Now you can learn more about developing Functions using Visual Studio Code: