Partilhar via


Samples for using the Azure App Service Kudu REST API to programmatically manage files in your site

Information about the Kudu REST API is found here:

https://github.com/projectkudu/kudu/wiki/REST-API

The VFS API section contains examples for programmatically managing files and directories in the App Service.

 

Here is an example (written in PowerShell) for listing the files and folders in the wwwroot folder.

$username = "`$websitename";

$password = "password";

<# This is the password from the msdeploySite credentials in the publish profile.

You can get the publish profile by going to the Overview blade on your App Service, clicking ...More at the top of the blade, and then clicking Get publish profile.

#>

$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $username,$password)))

$userAgent = "powershell/1.0";

$apiUrl = "https://websitename.scm.azurewebsites.net/api/vfs/site/wwwroot/";

Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET -ContentType "application/json";

 

Here is an example that puts a local file from your machine into the App Service file system:

$filePath = "c:\temp\text.txt";

$apiUrl = "https://websitename.scm.azurewebsites.net/api/vfs/site/wwwroot/test.txt";

Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method PUT -InFile $filePath -ContentType "multipart/form-data";

 

Here is an example that gets a file from the App Service file system and puts it in an existing folder on your local machine:

$filePath = "c:\temp\text.txt";
$apiUrl = "https://websitename.scm.azurewebsites.net/api/vfs/site/wwwroot/test.txt";
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET -OutFile $filePath -ContentType "multipart/form-data";

 

Here is an example that gets a folder on the App Service file system (the wwwroot folder in this case) and puts its contents on your local machine as a zip file:

$filePath = "c:\temp\wwwroot.zip";
$apiUrl = "https://websitename.scm.azurewebsites.net/api/zip/site/wwwroot/";
Invoke-RestMethod -Uri $apiUrl -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)} -UserAgent $userAgent -Method GET -OutFile $filePath -ContentType "application/zip";