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

使用 Azure CLI 将文件共享装载到 Python 函数应用Mount a file share to a Python function app using Azure CLI

此 Azure Functions 示例脚本会创建一个函数应用,并会在 Azure 文件存储中创建一个共享。This Azure Functions sample script creates a function app and creates a share in Azure Files. 然后,它会装载共享,使函数可以访问数据。It them mounts the share so that the data can be accessed by your functions.

备注

创建的函数应用在 Python 版本 3.7 上运行。The function app created runs on Python version 3.7. Azure Functions 也支持 Python 版本 3.6 和 3.8Azure Functions also supports Python versions 3.6 and 3.8.

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

先决条件Prerequisites

  • Azure Cloud Shell 中使用 Bash 环境。Use the Bash environment in Azure Cloud Shell.

    在新窗口中启动 Cloud ShellLaunch Cloud Shell in a new window

  • 如果需要,请安装 Azure CLI 来运行 CLI 参考命令。If you prefer, install the Azure CLI to run CLI reference commands.

    • 如果使用的是本地安装,请使用 az login 命令登录到 Azure CLI。If you're using a local installation, sign in to the Azure CLI by using the az login command. 若要完成身份验证过程,请遵循终端中显示的步骤。To finish the authentication process, follow the steps displayed in your terminal. 有关其他登录选项,请参阅登录 Azure CLIFor additional sign-in options, see Sign in with the Azure CLI.

    • 出现提示时,请在首次使用时安装 Azure CLI 扩展。When you're prompted, install Azure CLI extensions on first use. 有关扩展详细信息,请参阅使用 Azure CLI 的扩展For more information about extensions, see Use extensions with the 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. 如果使用 Azure Cloud Shell,则最新版本已安装。If using Azure Cloud Shell, the latest version is already installed.

示例脚本Sample script

此脚本使用消耗计划在 Azure Functions 中创建函数应用。This script creates a function app in Azure Functions using the Consumption plan.

#!/bin/bash

# Function app and storage account names must be unique.
export AZURE_STORAGE_ACCOUNT=mystorageaccount$RANDOM
functionAppName=myserverlessfunc$RANDOM
region=westeurope
pythonVersion=3.7 #3.6 also supported
shareName=myfileshare
directoryName=mydir
shareId=myshare$RANDOM
mountPath=/mounted

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

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

# Set the storage account key as an environment variable. 
export AZURE_STORAGE_KEY=$(az storage account keys list -g myResourceGroup -n $AZURE_STORAGE_ACCOUNT --query '[0].value' -o tsv)

# Create a serverless function app in the resource group.
az functionapp create \
  --name $functionAppName \
  --storage-account $AZURE_STORAGE_ACCOUNT \
  --consumption-plan-location $region \
  --resource-group myResourceGroup \
  --os-type Linux \
  --runtime python \
  --runtime-version $pythonVersion \
  --functions-version 2

# Work with Storage account using the set env variables.
# Create a share in Azure Files.
az storage share create \
  --name $shareName 

# Create a directory in the share.
az storage directory create \
  --share-name $shareName \
  --name $directoryName

az webapp config storage-account add \
  --resource-group myResourceGroup \
  --name $functionAppName \
  --custom-id $shareId \
  --storage-type AzureFiles \
  --share-name $shareName \
  --account-name $AZURE_STORAGE_ACCOUNT \
  --mount-path $mountPath \
  --access-key $AZURE_STORAGE_KEY

az webapp config storage-account list \
  --resource-group myResourceGroup \
  --name $functionAppName

  

清理部署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

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

命令Command 说明Notes
az group createaz group create 创建用于存储所有资源的资源组。Creates a resource group in which all resources are stored.
az storage account createaz storage account create 创建 Azure 存储帐户。Creates an Azure Storage account.
az functionapp createaz functionapp create 创建 Function App。Creates a function app.
az storage share createaz storage share create 在存储帐户中创建 Azure 文件存储共享。Creates an Azure Files share in storage account.
az storage directory createaz storage directory create 在共享中创建目录。Creates a directory in the share.
az webapp config storage-account addaz webapp config storage-account add 将共享装载到函数应用。Mounts the share to the function app.
az webapp config storage-account listaz webapp config storage-account list 显示装载到函数应用的文件共享。Shows file shares mounted to 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.