Setting Up a CDN on Azure and Using it With Both Visual Studio 2013 and a Node.js Server

The first thing you need to do is head about to azures management portal and log in: http://azure.microsoft.com/en-us/account/:
azure-login

Click on the plus sign for create new account at the bottom of the page:
new-page

Select Data Services, Storage, Quick Create:
new-storage

Choose a name for your storage service and select the location closest to you.

choose-name

Once created select click on the plus sign at the bottom in order to create another account:

new-page

Select app services, cdn, quick create, and select the storage account you just created:

new-cdn

Select the cdn and click on enable query strings at the bottom:

enable-querystring

Now open up Microsoft visual studio 2013 and open up server explorer:

server-explorer

Right click it select connect to Microsoft Azure Subscription and Log In:

log-in

Select Storage, CDN, Blobs:

cdn-blobs

Right click blobs and select create Blob Container and name it whatever you’d like:

container

On the window the tab that opens up click on the upload arrow to upload any files you want:

Now that your blob storage has been created and you are now able to populate it with whatever files you wish, let’s take a look at how to access those files from a node server and upload news files.

containers

Let’s start by installing azure-storage: npm install azure-storage. Make sure to add AZURESTORAGEACCOUNT and AZURESTORAGEACCESS_KEY to your environmental variables. You can get the storage access key and the storage account name from the management portal where you created the account.

access

Go to you storage account, select dashboard and at the bottom you’ll see the options manage access keys
If you click on it the following window will pop up. Copy and paste the account name and access keys into your environmental variables.

keys

Now, on the node application lets add the following code from this Microsoft azure post which allows us to add, download, and delete files from the CDN storage:

var azure = require('azure-storage');
var fs = require('fs');
var blobSvc = azure.createBlobService();
blobSvc.createContainerIfNotExists('test', function(error, result, response){
  if(!error){
    // Container exists and allows
    // anonymous read access to blob
    // content and metadata within this container
  }
});
blobSvc.createBlockBlobFromLocalFile('mycontainer', 'myblob', 'test.txt', function(error, result, response){
  if(!error){
    // file uploaded
  }
});
blobSvc.getBlobToStream('mycontainer', 'myblob', fs.createWriteStream('output.txt'), function(error, result, response){
  if(!error){
    // blob retrieved
  }
});
blobSvc.deleteBlob(containerName, 'myblob', function(error, response){
  if(!error){
    // Blob has been deleted
  }
});

Now your project should be all set up and ready to use a Azure’s CDN storage to host your static files.

Paulo Diniz