此 Azure Functions 示例脚本使用消耗计划创建函数应用,并在Azure 文件存储中创建共享。 然后,它会装载共享,使函数可以访问数据。
注意
创建的函数应用在 Python 版本 3.9 上运行。 Azure Functions 也支持 Python 版本 3.7 和 3.8。
如果没有 Azure 订阅,可在开始前创建一个试用帐户。
先决条件
可以使用本地 Azure CLI。
- 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。 
- 本地 Azure CLI,请了解如何安装 Azure CLI。 如果在 Windows 或 macOS 上运行,请考虑在 Docker 容器中运行 Azure CLI。 有关详细信息,请参阅如何在 Docker 容器中运行 Azure CLI。 - 通过使用 az login 命令登录到 Azure CLI。 若要完成身份验证过程,请遵循终端中显示的步骤。 有关其他登录选项,请参阅使用 Azure CLI 登录。 
- 出现提示时,请在首次使用时安装 Azure CLI 扩展。 有关扩展详细信息,请参阅使用 Azure CLI 的扩展。 
- 运行 az version 以查找安装的版本和依赖库。 若要升级到最新版本,请运行 az upgrade。 
 
示例脚本
运行脚本
#!/bin/bash
# <FullScript>
# Function app and storage account names must be unique.
# Variable block
let "randomIdentifier=$RANDOM*$RANDOM"
location="chinanorth2"
resourceGroup="msdocs-azure-functions-rg-$randomIdentifier"
tag="functions-cli-mount-files-storage-linux"
export AZURE_STORAGE_ACCOUNT="msdocsstorage$randomIdentifier"
functionApp="msdocs-serverless-function-$randomIdentifier"
skuStorage="Standard_LRS"
functionsVersion="4"
pythonVersion="3.9" #Allowed values: 3.7, 3.8, and 3.9
share="msdocs-fileshare-$randomIdentifier"
directory="msdocs-directory-$randomIdentifier"
shareId="msdocs-share-$randomIdentifier"
mountPath="/mounted-$randomIdentifier"
# Create a resource group
echo "Creating $resourceGroup in "$location"..."
az group create --name $resourceGroup --location "$location" --tags $tag
# Create an Azure storage account in the resource group.
echo "Creating $AZURE_STORAGE_ACCOUNT"
az storage account create --name $AZURE_STORAGE_ACCOUNT --location "$location" --resource-group $resourceGroup --sku $skuStorage
# Set the storage account key as an environment variable. 
export AZURE_STORAGE_KEY=$(az storage account keys list -g $resourceGroup -n $AZURE_STORAGE_ACCOUNT --query '[0].value' -o tsv)
# Create a serverless function app in the resource group.
echo "Creating $functionApp"
az functionapp create --name $functionApp --storage-account $AZURE_STORAGE_ACCOUNT --consumption-plan-location "$location" --resource-group $resourceGroup --os-type Linux --runtime python --runtime-version $pythonVersion --functions-version $functionsVersion
# Work with Storage account using the set env variables.
# Create a share in Azure Files.
echo "Creating $share"
az storage share create --name $share 
# Create a directory in the share.
echo "Creating $directory in $share"
az storage directory create --share-name $share --name $directory
# Create webapp config storage account
echo "Creating $AZURE_STORAGE_ACCOUNT"
az webapp config storage-account add \
--resource-group $resourceGroup \
--name $functionApp \
--custom-id $shareId \
--storage-type AzureFiles \
--share-name $share \
--account-name $AZURE_STORAGE_ACCOUNT \
--mount-path $mountPath \
--access-key $AZURE_STORAGE_KEY
# List webapp storage account
az webapp config storage-account list --resource-group $resourceGroup --name $functionApp
# </FullScript>
# echo "Deleting all resources"
# az group delete --name $resourceGroup -y
清理资源
使用 az group delete 命令删除资源组以及与其关联的所有资源 - 除非你持续需要这些资源。 其中一些资源在创建和删除时可能要稍等片刻。
az group delete --name $resourceGroup
示例参考
表中的每条命令均链接到特定于命令的文档。 此脚本使用以下命令:
| 命令 | 说明 | 
|---|---|
| az group create | 创建用于存储所有资源的资源组。 | 
| az storage account create | 创建 Azure 存储帐户。 | 
| az functionapp create | 创建 Function App。 | 
| az storage share create | 在存储帐户中创建 Azure 文件存储共享。 | 
| az storage directory create | 在共享中创建目录。 | 
| az webapp config storage-account add | 将共享装载到函数应用。 | 
| az webapp config storage-account list | 显示装载到函数应用的文件共享。 | 
后续步骤
有关 Azure CLI 的详细信息,请参阅 Azure CLI 文档。
可以在 Azure Functions 文档中找到其他 Azure Functions CLI 脚本示例。