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

创建由 Azure Blob 存储触发的函数Create a function triggered by Azure Blob storage

了解如何创建在文件上传到 Azure Blob 存储或在 Azure Blob 存储中更新时触发的函数。Learn how to create a function triggered when files are uploaded to or updated in Azure Blob storage.

查看日志中的消息。

先决条件Prerequisites

创建 Azure Function AppCreate an Azure Function app

  1. 选择 Azure 门户左上角的“创建资源”按钮,然后选择“计算” > “函数应用”。Select the Create a resource button found on the upper left-hand corner of the Azure portal, then select Compute > Function App.

    在 Azure 门户中创建函数应用

  2. 使用图像下面的表格中指定的函数应用设置。Use the function app settings as specified in the table below the image.

    定义新的函数应用设置

    设置Setting 建议的值Suggested value 说明Description
    应用名称App name 全局唯一名称Globally unique name 用于标识新 Function App 的名称。Name that identifies your new function app. 有效的字符是 a-z0-9-Valid characters are a-z, 0-9, and -.
    订阅Subscription 订阅Your subscription 要在其下创建此新函数应用的订阅。The subscription under which this new function app is created.
    资源组Resource Group myResourceGroupmyResourceGroup 要在其中创建 Function App 的新资源组的名称。Name for the new resource group in which to create your function app.
    OSOS WindowsWindows Linux 上的无服务器托管目前为预览版。Serverless hosting on Linux is currently in preview. 有关详细信息,请参阅此注意事项文章For more information, see this considerations article.
    托管计划Hosting plan 消耗量计划Consumption plan 定义如何将资源分配给 Function App 的托管计划。Hosting plan that defines how resources are allocated to your function app. 在默认的消耗计划中,根据函数需求动态添加资源。In the default Consumption Plan, resources are added dynamically as required by your functions. 在此无服务器托管中,只需为函数运行时间付费。In this serverless hosting, you only pay for the time your functions run. 按应用服务计划运行时,必须管理函数应用的缩放When you run in an App Service plan, you must manage the scaling of your function app.
    位置Location 西欧West Europe 选择离你近或离函数访问的其他服务近的区域Choose a region near you or near other services your functions access.
    运行时堆栈Runtime stack 首选语言Preferred language 选择支持你喜欢的函数编程语言的运行时。Choose a runtime that supports your favorite function programming language. 对于 C# 和 F# 函数,选择 .NETChoose .NET for C# and F# functions.
    存储Storage 全局唯一名称Globally unique name 创建函数应用使用的存储帐户。Create a storage account used by your function app. 存储帐户名称必须为 3 到 24 个字符,并且只能包含数字和小写字母。Storage account names must be between 3 and 24 characters in length and may contain numbers and lowercase letters only. 也可使用现有帐户,但该帐户必须符合存储帐户要求You can also use an existing account, which must meets the storage account requirements.
    Application InsightsApplication Insights 默认Default 在最近的受支持的区域中,创建一个具有相同应用名称的 Application Insights 资源。Creates an Application Insights resource of the same App name in the nearest supported region. 展开此设置即可更改“新建资源名称”,或者在 Azure 地理位置中选择另一个需要在其中存储数据的位置By expanding this setting, you can change the New resource name or choose a different Location in an Azure geography where you want to store your data.
  3. 选择“创建”以预配和部署函数应用。Select Create to provision and deploy the function app.

  4. 选择门户右上角的“通知”图标,留意是否显示“部署成功”消息。Select the Notification icon in the upper-right corner of the portal and watch for the Deployment succeeded message.

    定义新的函数应用设置

  5. 选择“转到资源”,查看新的函数应用。Select Go to resource to view your new function app. 还可选择“固定到仪表板”。You can also select Pin to dashboard. 固定可以更轻松地从仪表板返回此函数应用资源。Pinning makes it easier to return to this function app resource from your dashboard.

已成功创建 Function App。

接下来,在新的 Function App 中创建一个函数。Next, you create a function in the new function app.

创建 Blob 存储触发的函数Create a Blob storage triggered function

  1. 展开 Function App,单击“Functions”旁边的 + 按钮。Expand your function app and click the + button next to Functions. 如果这是函数应用中的第一个函数,请依次选择“门户中”、“继续”。If this is the first function in your function app, select In-portal then Continue. 否则,请转到第三步。Otherwise, go to step three.

    Azure 门户中的 Functions 快速入门页

  2. 依次选择“更多模板”、“完成并查看模板”。Choose More templates then Finish and view templates.

    Functions 快速入门选择更多模板

  3. 在搜索字段中,键入 blob,然后选择“Blob 触发器”模板。In the search field, type blob and then choose the Blob trigger template.

  4. 如果系统提示,请选择“安装”以在函数应用中安装 Azure 存储扩展的任何依赖项。If prompted, select Install to install the Azure Storage extension any dependencies in the function app. 安装成功后,选择“继续”。After installation succeeds, select Continue.

    安装绑定扩展

  5. 使用图像下的表中指定的设置。Use the settings as specified in the table below the image.

    创建 Blob 存储触发的函数。

    设置Setting 建议的值Suggested value 说明Description
    名称Name 在 Function App 中唯一Unique in your function app 此 Blob 触发函数的名称。Name of this blob triggered function.
    路径Path samples-workitems/{name}samples-workitems/{name} 所监视的 Blob 存储中的位置。Location in Blob storage being monitored. blob 的文件名将作为 name 参数传入绑定。The file name of the blob is passed in the binding as the name 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.
  6. 单击“创建”以创建函数。Click Create to create your function.

接下来,连接到 Azure 存储帐户并创建 samples-workitems 容器。Next, you connect to your Azure Storage account and create the samples-workitems container.

创建容器Create the container

  1. 在函数中,单击“集成”,展开“文档”,并复制帐户名称帐户密钥In your function, click Integrate, expand Documentation, and copy both Account name and Account key. 使用这些凭据连接到存储帐户。You use these credentials to connect to the storage account. 如果已连接存储帐户,请跳到步骤 4。If you have already connected your storage account, skip to step 4.

    获取存储帐户连接凭据。

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

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

  3. 输入步骤 1 中的帐户名称帐户密钥,单击“下一步”,并单击“连接”。Enter the Account name and Account key from step 1, click Next and then Connect.

    输入存储凭据和连接。

  4. 展开附加的存储帐户,右键单击“Blob 容器”,单击“创建 blob 容器”,键入 samples-workitems,并按 Enter。Expand the attached storage account, right-click Blob containers, click Create blob container, type samples-workitems, and then press enter.

    创建存储队列。

现在已有 blob 容器,可以通过将文件上传到该容器来测试函数。Now that you have a blob container, you can test the function by uploading a file to the container.

测试函数Test the function

  1. 返回到 Azure 门户中,浏览到函数,展开页面底部的“日志”并确保日志流式处理未暂停。Back in the Azure portal, browse to your function expand the Logs at the bottom of the page and make sure that log streaming isn't paused.

  2. 在存储资源管理器中,依次展开存储帐户、“Blob 容器”和 samples-workitemsIn Storage Explorer, expand your storage account, Blob containers, and samples-workitems. 依次单击“上传”、“上传文件...”。Click Upload and then Upload files....

    将文件上传到 blob 容器。

  3. 在“上传文件”对话框中,单击“文件”字段。In the Upload files dialog box, click the Files field. 浏览到本地计算机上的文件(如图像文件),选择它并单击“打开”,并单击“上传”。Browse to a file on your local computer, such as an image file, select it and click Open and then Upload.

  4. 返回到函数日志并验证是否已读取 blob。Go back to your function logs and verify that the blob has been read.

    查看日志中的消息。

    备注

    当 Function App 在默认消耗计划中运行时,添加或更新 blob 与触发函数之间可能会有多达几分钟的延迟。When your function app runs in the default Consumption plan, there may be a delay of up to several minutes between the blob being added or updated and the function being triggered. 如果需要在 blob 触发的函数中降低延迟,请考虑在应用服务计划中运行 Function App。If you need low latency in your blob triggered functions, consider running your function app in an App Service plan.

清理资源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

已创建在 Blob 存储中添加或更新 blob 时运行的函数。You have created a function that runs when a blob is added to or updated in Blob storage. 有关 Blob 存储触发器的详细信息,请参阅 Azure Functions Blob 存储绑定For more information about Blob storage triggers, see Azure Functions Blob storage bindings.

创建第一个函数以后,即可向这个可以将消息写入存储队列的函数添加输出绑定。Now that you have created your first function, let's add an output binding to the function that writes a message to a Storage queue.