將 App Service 應用程式連線至儲存體帳戶Connect an App Service app to a storage account

在此案例中,您將了解如何建立 Azure 儲存體帳戶和 App Service 應用程式。In this scenario you will learn how to create an Azure storage account and an App Service app. 然後,您會使用應用程式設定來將儲存體帳戶連結到該應用程式。Then you will link the storage account to the app using app settings.

您可以視需要使用 Azure PowerShell 指南 (英文) 中的指示來安裝 Azure PowerShell,然後執行 Connect-AzAccount 來建立與 Azure 的連線。If needed, install the Azure PowerShell using the instruction found in the Azure PowerShell guide, and then run Connect-AzAccount to create a connection with Azure.

範例指令碼Sample script

注意

本文已更新為使用 Azure Az PowerShell 模組。This article has been updated to use the Azure Az PowerShell module. Az PowerShell 模組是用來與 Azure 互動的建議 PowerShell 模組。The Az PowerShell module is the recommended PowerShell module for interacting with Azure. 若要開始使用 Az PowerShell 模組,請參閱安裝 Azure PowerShellTo get started with the Az PowerShell module, see Install Azure PowerShell. 若要瞭解如何遷移至 Az PowerShell 模組,請參閱將 Azure PowerShell 從 AzureRM 遷移至 Az。To learn how to migrate to the Az PowerShell module, see Migrate Azure PowerShell from AzureRM to Az.


# Generates a Random Value
$Random=(New-Guid).ToString().Substring(0,8)

# Variables
$ResourceGroup="MyResourceGroup$Random"
$AppName="webappwithStorage$Random"
$StorageName="webappstorage$Random"
$Location="West US"

# Create a Resource Group
New-AzResourceGroup -Name $ResourceGroup -Location $Location

# Create an App Service Plan
New-AzAppservicePlan -Name WebAppwithStoragePlan -ResourceGroupName $ResourceGroup -Location $Location -Tier Basic

# Create a Web App in the App Service Plan
New-AzWebApp -Name $AppName -ResourceGroupName $ResourceGroup -Location $Location -AppServicePlan WebAppwithStoragePlan

# Create Storage Account
New-AzStorageAccount -Name $StorageName -ResourceGroupName $ResourceGroup -Location $Location -SkuName Standard_LRS

# Get Connection String for Storage Account
$StorageKey=(Get-AzStorageAccountKey -ResourceGroupName $ResourceGroup -Name $StorageName).Value[0]

# Assign Connection String to App Setting 
Set-AzWebApp -ConnectionStrings @{ MyStorageConnStr = @{ Type="Custom"; Value="DefaultEndpointsProtocol=https;AccountName=$StorageName;AccountKey=$StorageKey;" } } -Name $AppName -ResourceGroupName $ResourceGroup

清除部署Clean up deployment

執行指令碼範例之後,您可以使用下列命令來移除資源群組、App Service 應用程式及所有相關資源。After the script sample has been run, the following command can be used to remove the resource group, App Service app, and all related resources.

Remove-AzResourceGroup -Name myResourceGroup -Force

指令碼說明Script explanation

此指令碼會使用下列命令。This script uses the following commands. 下表中的每個命令都會連結至命令特定的文件。Each command in the table links to command specific documentation.

CommandCommand 注意Notes
New-AzResourceGroupNew-AzResourceGroup 建立用來存放所有資源的資源群組。Creates a resource group in which all resources are stored.
New-AzAppServicePlanNew-AzAppServicePlan 建立 App Service 方案。Creates an App Service plan.
New-AzWebAppNew-AzWebApp 建立 App Service 應用程式。Creates an App Service app.
New-AzStorageAccountNew-AzStorageAccount 建立儲存體帳戶。Creates a Storage account.
Get-AzStorageAccountKeyGet-AzStorageAccountKey 取得 Azure 儲存體帳戶的存取金鑰。Gets the access keys for an Azure Storage account.
Set-AzWebAppSet-AzWebApp 修改 App Service 應用程式的設定。Modifies an App Service app's configuration.

後續步驟Next steps

如需有關 Azure PowerShell 模組的詳細資訊,請參閱 Azure PowerShell 文件For more information on the Azure PowerShell module, see Azure PowerShell documentation.

您可以在 Azure PowerShell 範例中找到適用於 Azure App Service 的其他 Azure PowerShell 範例。Additional Azure Powershell samples for Azure App Service can be found in the Azure PowerShell samples.