0
votes

I would like to move a file within an Azure App Service from a script. I thought that Azure CLI with az webapp command could be used for that, but could not find a way to access the files.

I know that there is Kudu REST API for file access, but I want to run the script during an Azure Devops Pipeline, so authentication for the REST API seems like an issue - I don't want to store the credentials in the pipeline. The same holds for FTP access. However, there is an Azure CLI task with authorization through configured Service connections, so I thought this might be the way.

So, the core question is - How to move a file within Azure App Service from a command line?

1
I don't think it's possible to copy files within the App service from the Azure CLI to other places. Maybe you need to use persistent shared storage for App service.Charles Xu

1 Answers

0
votes

So, it turned out that I can use Azure CLI to get Publishing profile for a particular App service (or even slot) and then use the user name and password from it to access Kudu REST API.

I used this Powershell 6 script

function Get-PublishingProfile($resourceGroupName, $webAppName, $slotName) {
  if ([string]::IsNullOrWhiteSpace($slotName)) {
    $xml = Get-AzWebAppPublishingProfile -ResourceGroupName $resourceGroupName -Name $webAppName
  }
  else {
    $xml = Get-AzWebAppSlotPublishingProfile -ResourceGroupName $resourceGroupName -Name $webAppName -Slot $slotName
  }

  return $xml |
  Select-Xml -XPath "publishData/publishProfile[1]" |
  Select-Object -ExpandProperty Node |
  Select-Object -Property publishUrl, msdeploySite, userName, userPWD, destinationAppUrl
}

$profile = Get-PublishingProfile $resourceGroupName $webAppName $slotName
$securePassword = ConvertTo-SecureString -String $profile.userPWD -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential($profile.userName, $securePassword)

$fileUri = "https://$($profile.publishUrl)/api/vfs/site/wwwroot/some_file.txt"
$headers = @{
  "If-Match" = "*"
}

Invoke-RestMethod -Uri $fileUri -Method Get -Credential $credentials -Authentication Basic -Headers $headers