首页 > 解决方案 > 如何使用powershell从远程zip文件部署functionapp

问题描述

我想使用 zip 文件在 azure 中部署一个 functionApp,该文件位于另一个使用 powershell 的 azure blob 存储中。我尝试了以下方法

#PowerShell
$username = "<deployment_user>"
$password = "<deployment_password>"
$filePath = "https://xxxxx.blob.core.windows.net/container/zzzz.zip"
$apiUrl = "https://<app_name>.scm.azurewebsites.net/api/zipdeploy"
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f 
$username, $password)))
$userAgent = "powershell/1.0"
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method POST -InFile $filePath - ContentType "multipart/form-data"

但是我收到以下错误消息,例如

Invoke-RestMethod:找不到驱动器。名为“https”的驱动器不存在。

如何从远程 url 文件进行部署?

标签: azurepowershell

解决方案


由于Invoke-RestMethod InFile接受来自文件的内容,因此需要先显式下载 blob 内容。

选项1

$fileUrl = "https://xxxxx.blob.core.windows.net/container/zzzz.zip"
$token = 'sp=r&st=2019-07-06T21:41:45Z&se=2019-07-07T05:41:45Z&spr=https&sv=2018-03-28&sig=9ud%2FiJ6GBccxZfyrKsZtP69lwuralu1D0QiiESa%2FXgo%3D&sr=b'
$filePath = [System.IO.Path]::GetFileName($fileUrl)
Invoke-WebRequest ('{0}?{1}' -f $fileUrl, $token) -OutFile $filePath  

先决条件

需要提供SAS(共享访问签名)URI而不是资源 URL,其中包括 SAS 令牌以执行经过身份验证的 请求

选项 2

通过Azure 存储 Cmdlet

$StorageAccountName = 'yourstorageaccount'
$StorageAccountKey = Get-AzureStorageKey -StorageAccountName $StorageAccountName
$StorageContext = New-AzureStorageContext $StorageAccountName -StorageAccountKey $StorageAccountKey.Primary
$FileName = 'zzzz.zip'
$OutputPath = 'C:\Temp'
$ContainerName  = 'yourcontainer'
Get-AzureStorageBlobContent -Blob $FilebName -Container $ContainerName -Destination $OutputPath -Context $StorageContext

推荐阅读