All we are perfectly familiar with the purpose of this technology as a CDN. According to the definition from Wikipedia Content Delivery Network - is a geographically distributed network infrastructure to optimize the delivery and distribution of content to end users on the Internet. Today I begin a series of posts on the Microsoft CDN. In one of my previous articles, I briefly mentioned the possibility of using Windows Azure CDN for static content from a different store blobs, today we are more acquainted with how and by what means it can be done.
As an example of using the CDN will demonstrate a simple Web-based application with a single page containing the stylesheet engine, and JQuery. To begin with all the static content will be stored on the server next to the application in the future we will move it to the store blobs and eventually get access to it through the Azure CDN.
So, first create a blank standard ASP. NET MVC application, and three slightly podredaktiruem damper, which is used on the home page. Let's look at the code view.
@ using MvcApplication1. Models.
@ {.
ViewBag. Title = 'Home Page';.
}.
@ section Header.
{.
}.
@ ViewBag. Message.
To learn more about ASP. NET MVC visit. http://asp. net / mvc.
As you can see I used the extension- method StaticContent for links to static content and to simplify the generation of URLs in the future it is very useful. The code extends the method presented below listing.
using System;.
using System. Collections. Generic;.
using System. Linq;.
using System. Web;.
using System. Web. WebPages. Html;.
using System. Web. Mvc;.
namespace MvcApplication1. Models.
{.
public static class UrlHelperExtensions.
{.
public static string StaticContent (this UrlHelper helper, string resource).
{.
return helper. Content (resource);.
}.
}.
}.
At the moment all the static is on our server. But we want to get it out of the cloud. To download the content in the repository I used blobs cmdlet PowerShell, freely distributed on the Internet. Use the supplied together with a configuration file in which to register the subscription ID, name of the storage- account, the key to access the repository, the name of the target container and the original path to the folder where the downloaded files are stored. For clarity, let's look at this file.
If you are familiar with PowerShell cmdlet itself as nothing special surprise. Here is the code.
function UploadBlobs ($ container, $ directory, $ virtualPath).
{.
Write-Output 'Uploading $ directory files. '.
Foreach ($ file in Get-ChildItem $ directory).
{.
if ($ file. PSIsContainer).
{.
UploadBlobs $ container $ file. fullname ($ virtualPath $ file. name '/').
}.
else.
{.
Write-Output 'Uploading $ file'.
$ blob = $ container. GetBlobReference ($ virtualPath $ file. name).
$ blob. Properties. ContentType = GetContentTypeFromExtension $ file. extension.
$ blob. Properties. CacheControl = 'public, max-age = 86400 '.
$ blob. UploadFile ($ file. fullName).
}.
}.
}.
function SetCacheControlNoCache ($ container, $ resource).
{.
$ blob = $ container. GetBlobReference ($ resource).
$ blob. Properties. CacheControl = 'public, max-age = 0';.
$ blob. SetProperties ().
}.
function GetContentTypeFromExtension ([string] $ extension).
{.
switch ($ extension).
{.
'. png '{return' image / png '}.
'. htm '{return' text / html '}.
'. pfx '{return' application/x-pkcs12 '}.
'. xml '{return' text / xml '}.
'. css' {return 'text / css'}.
'. jpg '{return' image / jpeg '}.
'. jpeg '{return' image / jpeg '}.
'. bmp '{return' image / bmp '}.
'. js' {return 'text / x-javascript'}.
'. zip '{return' application / zip '}.
}.
Write-Output 'application / octet-stream'.
}.
$ scriptDir = (split-path $ myinvocation. mycommand. path-parent).
Set-Location $ scriptDir.
$ sdkPath = resolve-path '$ Env: ProgramFiles \\ Windows Azure SDK \\ v1. ? \\ ref '.
write-host $ sdkPath.
if ($ sdkPath-is [Array]).
{$ RefFolder = $ sdkPath [-1]}.
else.
{$ refFolder = [string] $ sdkPath}.
[Reflection. Assembly]:: LoadFile ($ refFolder '\\ Microsoft. WindowsAzure. StorageClient. dll ').
[xml] $ xml = get-Content '. / configuration. xml '.
$ subId = $ xml. settings. subscriptionId.
$ storageAccount = $ xml. settings. storageAccount. name.
$ storageAccountKey = $ xml. settings. storageAccount. key.
$ containerName = $ xml. settings. containerName.
$ sourceFolder = $ xml. settings. sourceFolder.
Write-Host 'Uploading files. '.
$ credentials = New-Object Microsoft. WindowsAzure. StorageCredentialsAccountAndKey-ArgumentList $ storageAccount, $ storageAccountKey.
$ account = New-Object Microsoft. WindowsAzure. CloudStorageAccount-ArgumentList $ credentials, TRUE.
$ client = [Microsoft. WindowsAzure. StorageClient. CloudStorageAccountStorageClientExtensions]:: CreateCloudBlobClient ($ account).
$ timeout = New-Object System. TimeSpan-ArgumentList 0, 10, 0.
this allows us to upload large blobs.
$ client. Timeout = $ timeout.
$ container = $ client. GetContainerReference ($ containerName).
$ container. CreateIfNotExist ().
$ publicPermission = New-Object Microsoft. WindowsAzure. StorageClient. BlobContainerPermissions.
$ publicPermission. PublicAccess = [Microsoft. WindowsAzure. StorageClient. BlobContainerPublicAccessType]:: Blob.
$ container. SetPermissions ($ publicPermission).
UploadBlobs $ container $ sourceFolder''.
SetCacheControlNoCache $ container 'images / Destination. jpg '.
Write-Host 'Done!'.
Once the files are successfully loaded, modify the code slightly extends our method, namely, redirect all links to store blobs instead of local server.
using System;.
using System. Collections. Generic;.
using System. Linq;.
using System. Web;.
using System. Web. WebPages. Html;.
using System. Web. Mvc;.
namespace MvcApplication1. Models.
{.
public static class UrlHelperExtensions.
{.
public static string StaticContent (this UrlHelper helper, string resource).
{.
return helper. Content (String. Format ('https://feschenkoalex. blob. core. windows. net { 0 } ', resource));.
}.
}.
}.
Once launched, we see that the site is all static loads as well, but from the store blobs.
At the moment we moved all static content to a remote repository, but the goal stood in front of us to access files through the Windows Azure CDN. To do this, perform the following steps. The first step is to activate the access point, it is done in section CDN.
In the modal window to select the account in case if we have several of them and click OK (do not forget to leave it on tick. Enable CDN. ).
After that we sternly warn that the changes will take effect within 60 minutes. Wait until the point goes to the status of Enabled, and copy the address from the resulting properties of the panels CDN.
When all the setup procedures are completed you can return to the application code and then change the body of our method extends. At this time we will replace the domain name to store blobs CDN- domain.
using System;.
using System. Collections. Generic;.
using System. Linq;.
using System. Web;.
using System. Web. WebPages. Html;.
using System. Web. Mvc;.
namespace MvcApplication1. Models.
{.
public static class UrlHelperExtensions.
{.
public static string StaticContent (this UrlHelper helper, string resource).
{.
return helper. Content (String. Format ('https://az32162. vo. msecnd. net { 0 } ', resource));.
}.
}.
}.
If by this time our changes have spread around the world )), we obtain the content from the nearest CDN- site, otherwise you'll have to wait. In conclusion, results demonstrate the utility. tracert. for the domain and data source for the CDN- domain.
As you can see the number of network devices involved in the case of CDN is significantly reduced, allowing faster access to content.
No comments:
Post a Comment