如何使用PowerShell实现Azure Web App的文件上传与删除功能
问题排查
- 你遇到的405 Method Not Allowed报错,核心原因有两个:
- 直接向App Service的业务访问域名(格式为
xxx.azurewebsites.net)发送PUT/DELETE请求,默认Web服务不会开放文件写入、删除的HTTP方法权限 - 操作Azure App Service的文件系统必须进行身份验证,匿名请求没有文件修改权限
- 直接向App Service的业务访问域名(格式为
功能实现方案
可使用Azure App Service内置的KUDU VFS API完成文件的上传、下载、删除操作,完整可用的PowerShell代码如下:
# 获取App Service发布凭证 function Get-AzWebAppPublishingCredentials($resourceGroupName, $webAppName){ $resourceType = "Microsoft.Web/sites/config" $resourceName = "$webAppName/publishingcredentials" $publishingCredentials = Invoke-AzResourceAction -ResourceGroupName $resourceGroupName -ResourceType $resourceType -ResourceName $resourceName -Action list -ApiVersion 2015-08-01 -Force return $publishingCredentials } # 生成KUDU API身份验证头 function Get-KuduApiAuthorisationHeaderValue($resourceGroupName, $webAppName){ $publishingCredentials = Get-AzWebAppPublishingCredentials $resourceGroupName $webAppName return ("Basic {0}" -f [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $publishingCredentials.Properties.PublishingUserName, $publishingCredentials.Properties.PublishingPassword)))) } # 上传本地文件到App Service function Upload-FileToWebApp($resourceGroupName, $webAppName, $localPath, $kuduPath){ $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/$kuduPath" $virtualPath = $kuduApiUrl.Replace(".scm.azurewebsites.", ".azurewebsites.").Replace("/api/vfs/site/wwwroot", "") Write-Host "正在上传文件到App Service,本地路径:'$localPath',目标路径:'$virtualPath'" -ForegroundColor DarkGray Invoke-RestMethod -Uri $kuduApiUrl ` -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` -Method PUT ` -InFile $localPath ` -ContentType "multipart/form-data" } # 删除App Service内的文件 function Delete-FileFromWebApp($resourceGroupName, $webAppName, $kuduPath){ $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/$kuduPath" $virtualPath = $kuduApiUrl.Replace(".scm.azurewebsites.", ".azurewebsites.").Replace("/api/vfs/site/wwwroot", "") Write-Host "正在删除App Service文件,目标路径:'$virtualPath'" -ForegroundColor DarkGray Invoke-RestMethod -Uri $kuduApiUrl ` -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` -Method DELETE } # 从App Service下载文件到本地 function Download-FileFromWebApp($resourceGroupName, $webAppName, $kuduPath, $localPath){ $kuduApiAuthorisationToken = Get-KuduApiAuthorisationHeaderValue $resourceGroupName $webAppName $kuduApiUrl = "https://$webAppName.scm.azurewebsites.net/api/vfs/site/wwwroot/$kuduPath" $virtualPath = $kuduApiUrl.Replace(".scm.azurewebsites.", ".azurewebsites.").Replace("/api/vfs/site/wwwroot", "") Write-Host "正在从App Service下载文件,源路径:'$virtualPath',本地保存路径:'$localPath'" -ForegroundColor DarkGray Invoke-RestMethod -Uri $kuduApiUrl ` -Headers @{"Authorization"=$kuduApiAuthorisationToken;"If-Match"="*"} ` -Method GET ` -OutFile $localPath }
调用示例
使用前请先在PowerShell环境中安装Az模块并登录Azure账号:
# 登录Azure账号 Connect-AzAccount # 切换到目标资源所在的订阅(如有需要) Set-AzContext -SubscriptionId "你的订阅ID" # 定义参数 $resourceGroupName = "资源组名称" $webAppName = "App Service名称" $localFile = "D:\AzureSolution\local_backup\test.txt" $targetKuduPath = "azure_backup/test.txt" # 对应App Service wwwroot下的相对路径 # 调用函数示例 Upload-FileToWebApp $resourceGroupName $webAppName $localFile $targetKuduPath Download-FileFromWebApp $resourceGroupName $webAppName $targetKuduPath $localFile Delete-FileFromWebApp $resourceGroupName $webAppName $targetKuduPath
如果你还在使用旧版AzureRM模块,将代码中的
Invoke-AzResourceAction替换为Invoke-AzureRmResourceAction、Connect-AzAccount替换为Connect-AzureRmAccount即可使用。
内容的提问来源于stack exchange,提问作者Eric46900
相关产品推荐
相关产品推荐

