Fájlok feltöltése webalkalmazásba FTP használatával

Ez a példaszkript létrehoz egy webalkalmazást a App Service a kapcsolódó erőforrásokkal együtt, majd ftps használatával (a System.Net.FtpWebRequest használatával) üzembe helyez egy fájlt.

Ha szükséges, telepítse a Azure PowerShell a Azure PowerShell útmutatóban található utasítással, majd futtassa a parancsot Connect-AzAccount az Azure-ral való kapcsolat létrehozásához.

Példaszkript

Megjegyzés

Javasoljuk, hogy az Azure Az PowerShell-modult használja az Azure-ral való kommunikációhoz. Az első lépésekhez tekintse meg az Azure PowerShell telepítését ismertető szakaszt. Az Az PowerShell-modulra történő migrálás részleteiről lásd: Az Azure PowerShell migrálása az AzureRM modulból az Az modulba.

$filePath="<Replace with full file path>"
$webappname="mywebapp$(Get-Random)"
$location="West Europe"

# Create a resource group.
New-AzResourceGroup -Name myResourceGroup -Location $location

# Create an App Service plan in `Free` tier.
New-AzAppServicePlan -Name $webappname -Location $location `
-ResourceGroupName myResourceGroup -Tier Free

# Create a web app.
New-AzWebApp -Name $webappname -Location $location -AppServicePlan $webappname `
-ResourceGroupName myResourceGroup

# Get publishing profile for the web app
$xml = [xml](Get-AzWebAppPublishingProfile -Name $webappname `
-ResourceGroupName myResourceGroup `
-OutputFile null)

# Extract connection information from publishing profile
$username = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userName").value
$password = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@userPWD").value
$url = $xml.SelectNodes("//publishProfile[@publishMethod=`"FTP`"]/@publishUrl").value

# Upload file 
$file = Get-Item -Path $filePath
$uri = New-Object System.Uri("$url/$($file.Name)")

$request = [System.Net.FtpWebRequest]([System.net.WebRequest]::Create($uri))
$request.Method = [System.Net.WebRequestMethods+Ftp]::UploadFile
$request.Credentials = New-Object System.Net.NetworkCredential($username,$password)

# Enable SSL for FTPS. Should be $false if FTP.
$request.EnableSsl = $true;

# Write the file to the request object.
$fileBytes = [System.IO.File]::ReadAllBytes($filePath)
$request.ContentLength = $fileBytes.Length;
$requestStream = $request.GetRequestStream()

try {
    $requestStream.Write($fileBytes, 0, $fileBytes.Length)
}
finally {
    $requestStream.Dispose()
}

Write-Host "Uploading to $($uri.AbsoluteUri)"

try {
    $response = [System.Net.FtpWebResponse]($request.GetResponse())
    Write-Host "Status: $($response.StatusDescription)"
}
finally {
    if ($null -ne $response) {
        $response.Close()
    }
}

Az üzemelő példány eltávolítása

A példaszkript futtatása után a következő paranccsal távolítható el az erőforráscsoport, a webalkalmazás és az összes kapcsolódó erőforrás.

Remove-AzResourceGroup -Name myResourceGroup -Force

Szkript ismertetése

A szkript a következő parancsokat használja. A táblázatban lévő összes parancs a hozzá tartozó dokumentációra hivatkozik.

Parancs Jegyzetek
New-AzResourceGroup Létrehoz egy erőforráscsoportot, amely az összes erőforrást tárolja.
New-AzAppServicePlan Létrehoz egy App Service-csomagot.
New-AzWebApp Webalkalmazást hoz létre.
Get-AzWebAppPublishingProfile Lekéri egy webalkalmazás közzétételi profilját.

Következő lépések

Az Azure PowerShell modullal kapcsolatos további információért lásd az Azure PowerShell dokumentációját.

A Azure App Service Web Apps további Azure PowerShell mintái megtalálhatók a Azure PowerShell mintákban.