powershell Azure CDN

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell Azure CDN相关的知识,希望对你有一定的参考价值。

It can be hosted in Azure or any other location.

# Origins

With Azure CDN, you can cache static objects loaded from Azure Blob storage, a web application, or any publicly accessible web server, 
by using the closest point of presence (POP) server. 

Azure CDN can also accelerate dynamic content, which cannot be cached, by leveraging various network and routing optimizations.

# Profiles

You need to create at least one CDN profile, which is a collection of CDN endpoints.
Every CDN endpoint represents a specific configuration of content delivery behavior and access.

# Optimisation

CDN can optimize the delivery experience based on the type of content you have.
The content can be a website, a live stream, a video, or a large file for download.
When you create a CDN endpoint, you specify a scenario in the Optimized for option.

- General web delivery: Most common optimization option.
Designed for general web content optimization, such as webpages and web applications. 
Can be used for file and video downloads.
- General media streaming: If you need to use the endpoint for live streaming and video-on-demand streaming.
- Video-on-demand media streaming: Video-on-demand media streaming optimization improves video-on-demand streaming content.
- Large file download: Optimized for content larger than 10 MB.
- Dynamic site acceleration: Involves an additional fee.

Also offered via Azure Front Door Service.

Benefits the latency and performance of dynamic content.
Techniques include route and network optimization, TCP optimization, and more.
Create and upload the app to a new app service web app:

```
git clone https://github.com/Azure-Samples/html-docs-hello-world.git
cd html-docs-hello-world
az webapp up --location westeurope --name ric01-static
```

Returns:
```json
Creating Resource group 'richard.cowin_rg_Windows_westeurope' ...
Creating App service plan 'richard.cowin_asp_Windows_westeurope_0' ...
Creating app 'ric01-static' ...
Starting zip deployment.
{
  ...
  "name": "ric01-static",
  "os": "Windows",
  "resourcegroup": "richard.cowin_rg_Windows_westeurope"
  ...
}
```

This will also update the app after changes:
```
az webapp up --location westeurope --name ric01-static
```
Use Networking blade to add CDN profile:

Assets will be available at, e.g.:

- http://`<appname>`.azurewebsites.net/css/bootstrap.css
- http://`<endpointname>`.azureedge.net/css/bootstrap.css

The default TTL is seven days.

To trigger a refresh, manually purge the CDN resources using the Azure Portal.

Caching behavior options:
- Ignore query strings (default)
- Bypass caching for query strings
- Cache every unique URL
// Authenticate via the SDK, assuming AZURE_AUTH_LOCATION is path to auth file
// This is common for all SDKs
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
      .Configure()
      .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
      .Authenticate(credentials)
      .WithDefaultSubscription();

// Create resource group
azure.ResourceGroups.Define(rgName)
      .WithRegion(Region.USCentral)
      .Create();

// create a few web apps in several regions, each one be like:
var app = azure.WebApps
      .Define(appName)
      .WithRegion(Region.EuropeNorth)
      .WithExistingResourceGroup(rgName)
      .WithNewWindowsPlan(PricingTier.StandardS1)
      .WithJavaVersion(JavaVersion.V8Newest)
      .WithWebContainer(WebContainer.Tomcat8_0Newest)
      .DefineSourceControl()
          .WithPublicGitRepository("https://github.com/jianghaolu/azure-site-test")
          .WithBranch("master")
          .Attach()
      .Create();

// Create CDN profile using Standard Verizon SKU with endpoints in each region of Web apps
var profileDefinition = azure.CdnProfiles.Define(cdnProfileName)
      .WithRegion(Region.USSouthCentral)
      .WithExistingResourceGroup(rgName)
      .WithStandardVerizonSku();

// define all the endpoints - do this for each webapp
ICreatable<ICdnProfile> cdnCreatable =
    cdnCreatable = profileDefinition
        .DefineNewEndpoint()
            .WithOrigin(appName + ".azurewebsites.net")
            .WithHostHeader(appName + ".azurewebsites.net")
            .WithCompressionEnabled(true)
            .WithContentTypeToCompress("application/javascript")
            .WithQueryStringCachingBehavior(QueryStringCachingBehavior.IgnoreQueryString)
        .Attach();

// create profile and then all the defined endpoints in parallel
ICdnProfile profile = cdnCreatable.Create();

// Load some content (referenced by Web Apps) to the CDN endpoints.
var contentToLoad = new HashSet<string>();
contentToLoad.Add("/server.js");
contentToLoad.Add("/pictures/microsoft_logo.png");
foreach (ICdnEndpoint endpoint in profile.Endpoints.Values)
{
    endpoint.LoadContent(contentToLoad);
}
# Output the name of all profiles on this subscription.
Get-AzCdnProfile | ForEach-Object { Write-Host $_.Name }

# Get a single endpoint.
$endpoint = Get-AzCdnEndpoint -ProfileName CdnDemo -ResourceGroupName CdnDemoRG -EndpointName cdndocdemo

# Create a new profile
New-AzCdnProfile -ProfileName CdnPoshDemo `
  -ResourceGroupName CdnDemoRG `
  -Sku Standard_Akamai `
  -Location "Central US"

# Create a new endpoint
New-AzCdnEndpoint -ProfileName CdnPoshDemo `
  -ResourceGroupName CdnDemoRG `
  -Location "Central US" `
  -EndpointName cdnposhdoc `
  -OriginName "Contoso" `
  -OriginHostName "www.contoso.com"

# Retrieve availability
$availability = Get-AzCdnEndpointNameAvailability -EndpointName "cdnposhdoc"
$availability.NameAvailable

# Modify
$endpoint.IsCompressionEnabled = $true
$endpoint.ContentTypesToCompress = "text/javascript","text/css","application/json"
Set-AzCdnEndpoint -CdnEndpoint $endpoint

# Purge some assets
Unpublish-AzCdnEndpointContent -ProfileName CdnDemo `
  -ResourceGroupName CdnDemoRG `
  -EndpointName cdndocdemo `
  -PurgeContent "/images/kitten.png","/video/rickroll.mp4"

# Pre-load some assets
Publish-AzCdnEndpointContent -ProfileName CdnDemo `
  -ResourceGroupName CdnDemoRG `
  -EndpointName cdndocdemo `
  -LoadContent "/images/kitten.png","/video/rickroll.mp4"

# Stop an endpoint
Stop-AzCdnEndpoint -ProfileName CdnDemo `
  -ResourceGroupName CdnDemoRG `
  -EndpointName cdndocdemo

以上是关于powershell Azure CDN的主要内容,如果未能解决你的问题,请参考以下文章

Azure中国CDN全球覆盖功能初探

Microsoft Azure系列之四 Micorosft Azure Powershell 管理

如何在 azure blob 中获取文件的 azure cdn 链接?

使用 PowerShell 自动登录Azure

使用 PowerShell 自动登录 Azure

Azure PowerShell批量创建Azure虚拟机