创建具有命名存储帐户连接的函数应用Create a function app with a named Storage account connection

此 Azure Functions 示例脚本先创建一个函数应用,然后将该函数连接到 Azure 存储帐户。This Azure Functions sample script creates a function app and connects the function to an Azure Storage account. 创建的应用设置(包含连接)可以与存储触发器或绑定配合使用。The created app setting that contains the connection can be used with a storage trigger or binding.

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

先决条件Prerequisites

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.
    • 如果使用的是本地安装,请通过 Azure CLI 使用 az login 命令登录。If you're using a local install, sign in with Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅使用 Azure CLI 登录See Sign in with Azure CLI for additional sign-in options.
    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with Azure CLI.
    • 运行 az version 以查找安装的版本和依赖库。Run az version to find the version and dependent libraries that are installed. 若要升级到最新版本,请运行 az upgradeTo upgrade to the latest version, run az upgrade.
  • 本教程需要 Azure CLI 版本 2.0 或更高版本。This tutorial requires version 2.0 or later of the Azure CLI.

示例脚本Sample script

此示例创建 Azure Function app,并将存储连接字符串添加到应用设置。This sample creates an Azure Function app and adds the storage connection string to an app setting.

#!/bin/bash

# Function app and storage account names must be unique.
storageName="mystorageaccount$RANDOM"
functionAppName="myfuncwithstorage$RANDOM"
region=chinanorth2

# Create a resource group with location.
az group create \
  --name myResourceGroup \
  --location $region

# Create a storage account in the resource group.
az storage account create \
  --name $storageName \
  --location $region \
  --resource-group myResourceGroup \
  --sku Standard_LRS

# Create a serverless function app in the resource group.
az functionapp create \
  --name $functionAppName \
  --resource-group myResourceGroup \
  --storage-account $storageName \
  --consumption-plan-location $region \
  --functions-version 2

# Get the storage account connection string. 
connstr=$(az storage account show-connection-string --name $storageName --resource-group myResourceGroup --query connectionString --output tsv)

# Update function app settings to connect to the storage account.
az functionapp config appsettings set \
  --name $functionAppName \
  --resource-group myResourceGroup \
  --settings StorageConStr=$connstr

清理部署Clean up deployment

运行示例脚本后,可以使用以下命令删除资源组以及与其关联的所有资源。After the sample script has been run, the following command can be used to remove the resource group and all resources associated with it.

az group delete --name myResourceGroup

脚本说明Script explanation

此脚本使用以下命令。This script uses the following commands. 表中的每条命令均链接到特定于命令的文档。Each command in the table links to command specific documentation.

命令Command 说明Notes
az group createaz group create 使用相关位置创建资源组。Create a resource group with location.
az storage account createaz storage account create 创建存储帐户。Create a storage account.
az functionapp createaz functionapp create 在无服务器消耗计划中创建函数应用。Creates a function app in the serverless Consumption plan.
az storage account show-connection-stringaz storage account show-connection-string 获取帐户的连接字符串。Gets the connection string for the account.
az functionapp config appsettings setaz functionapp config appsettings set 将连接字符串设置为函数应用中的应用设置。Sets the connection string as an app setting in the function app.

后续步骤Next steps

有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档For more information on the Azure CLI, see Azure CLI documentation.

可以在 Azure Functions 文档中找到其他 Azure Functions CLI 脚本示例。Additional Azure Functions CLI script samples can be found in the Azure Functions documentation.