Can't copy ftp files from azure app service to local with powershell

MoTaar 310 Reputation points
2023-12-07T23:20:12.8233333+00:00

Is there a script that allows to copy ftp files from azure app service to local or to azure fileshare?

I tried several ones but none of them worked.

Thank you!

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,743 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,177 questions
{count} votes

1 answer

Sort by: Most helpful
  1. brtrach-MSFT 17,166 Reputation points Microsoft Employee
    2023-12-09T01:33:41.3766667+00:00

    @MoTaar To copy files from an Azure App Service to a local machine or to an Azure file share, you can use FTPS (FTP over SSL). Here's an example PowerShell script that uses FTPS to download files from an Azure App Service to a local machine:

    # Replace the values below with your own
    $resourceGroupName = "myResourceGroup"
    $appName = "myAppService"
    $username = "username"
    $password = "password"
    $localPath = "C:\local\path"
    $remotePath = "/site/wwwroot/remote/path"
    
    # Connect to the Azure App Service using FTPS
    $ftpsUri = "ftps://$appName.ftp.azurewebsites.windows.net/$remotePath"
    $ftpsRequest = [System.Net.FtpWebRequest]::Create($ftpsUri)
    $ftpsRequest.Credentials = New-Object System.Net.NetworkCredential($username, $password)
    $ftpsRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile
    $ftpsRequest.EnableSsl = $true
    
    # Download the files
    $response = $ftpsRequest.GetResponse()
    $stream = $response.GetResponseStream()
    $localFilePath = Join-Path $localPath (Split-Path $ftpsRequest.RequestUri.LocalPath -Leaf)
    $stream.ReadTimeout = 10000
    $buffer = New-Object byte[] 1024
    $fs = New-Object System.IO.FileStream($localFilePath,[System.IO.FileMode]::Create)
    while (($read = $stream.Read($buffer,0,$buffer.Length)) -gt 0) {
        $fs.Write($buffer,0,$read)
    }
    $fs.Close()
    $response.Close()
    
    Write-Host "Files downloaded to $localFilePath"
    
    
    

    Make sure to replace the values for $resourceGroupName, $appName, $username, $password, $localPath, and $remotePath with your own values.

    If you're still having trouble, please let me know and provide more details about the issues you're facing.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.