Personal URL Shortening Service with Azure Storage

So, you've got your own domain. Let's say azure123.com. Wouldn't you like to have a URL shortening service just for you? How would that work? What's the cheapest way to do it?

I always forget my work webmail address. I always forget my blog URL (not true). I always forget my friend's site url. I'd like to have a URL that I always remember, and I'd like to have total control over it. I also am poor, so I'd like to avoid having to pay for a website or for a web role. 

Here is one way to do it... using Azure Storage (and a little javascript)!

 

Step 1: Create a Windows Azure Storage Account

Do you have a Windows Azure subscription? Get one. You can use a trial account too. Login the management portal, click on the new button at the bottom left of the screen, select 'Data Services', 'Storage' and 'Quick Create'. Set a URL (you'll probably want to use your own custom domain later on), choose a data center closest to you. You can disable Geo-Replication if you want to shave off a little more off your usage (it won't matter really).

While the storage account is being created, you can start on step 2.

 

Step 2: Create your javascript redirector

So let's start work on the redirector itself. We'll have one for each url, but we can reuse most of it. Here's a sample javascript redirector:

<!DOCTYPE html>
<html>
<head></head>
<body>
<script>
// Where is Malta's Airport? Here!
var toUrl='http://binged.it/13dedtl';
window.location=toUrl;
</script>
</body>
</html>

For every redirector, just change the toUrl variable to point to the link you always forget, and save the file to a friendly name.

 

Step 3: Install a storage app

The next step is to get an application that will allow you to upload the redirector(s). You may want to use a dedicated app such as the CloudBerry Explorer with Windows Azure (http://www.cloudberrylab.com/free-microsoft-azure-explorer.aspx). It's a free tool that works well and is pretty easy to use. Otherwise, you can stick with Visual Studio with the latest Azure SDK to do so. Let's do it with Visual Studio.

 

Step 4: Upload the redirectors to the Storage Account

First, add the storage account to your server explorer. Right click on 'Windows Azure Storage', and select 'Add New Storage Account'.  You can enter the settings manually. All you need is the account name and account key, both can be found in the Azure Management Portal (find the azure account, and click on 'Manage Access Keys' from the bottom. 

Once the account has been created, expand it select 'Blobs' and create a new container. Here's an neat trick. Name the container '$root' (without the quotes). That will eliminate the container part from the final URL, making it as short as possible. We need this container to be publicly accessible so we need to switch this on. Right click on the container, select properties, find the 'PublicAccess' property and make sure it's set to 'Blob'.

Right click the container again and open it by clicking 'View Blob Container'. Great! Now find the upload icon (arrow pointing upwards) to upload your redirector files. I've saved mine as 'airport.html'. Upload the files in the $root container. Notice that the content-type is set to 'text/html'. This is important so that the javascript would be run by the browser, and not downloaded. If this doesn't happen on upload, you may want to check the file .html extension. If you'd like, you can use another azure storage tool (like the CloudBerry Explorer) to remove the extension (but keep the content-type) so that the final url would be even shorter. Havn't managed to find a way to do this last part in Visual Studio as yet! If you upload it without the extension, the content-type would be incorrect.

Till now, I got http://testme123.blob.core.windows.net/airport

 

Step 5: Associate your custom domain with Azure Storage

The above url is great, but it's a tad longer than my maximum memory limit (yeah, I know). I want it shorter! Shorter? I need a custom domain! If you've got a custom domain to play around with, we'll need to create a new DNS CNAME record. Let's say I own the domain azurerocks.com (which I don't), and I'd like to use this domain to add my cool redirector. I want the redirector to have the url http://urls.azure123.com/airport.

First, register the custom domain with Azure using the Management Portal. To access the dialog, select your storage account, and navigate to the 'Configure' tab. At the bottom, click on the 'Manage Domain' button. It will require an activation process before the domain is activated. The activation process would require you to register (temporarily) a long CNAME to point to verify.azure.com. Once this is done, click on the validate button to complete the registration. However, due to DNS propagation, it usually takes some time (even hours sometimes so have patience). 

 

Step 6: See it work!

Well, at this point you should have your redirector work with your custom domain. If you'd like to add new redirectors, just copy/paste the file and change the URL from the javascript. Remember that since we're not using any cloud services or azure website hosting, we're just using Azure storage, this redirector is dirt cheap. You'll be charged only for the bandwidth and the storage cost, which both are ridiculously cheap. In comparison, my test redirector was 188bytes. Storage and bandwidth costs are measured in Gigabytes. First 5GB of bandwidth is free as well, so there!

 

 

Here's what we've covered:

1. Setting up a Windows Azure Storage Account

2. Creating a simple redirector with javascript

3. Installing and using an Azure storage application

4. Adding the storage account to Visual Studio

5. Creating a new container. Name it $root to make it invisible in the url!

6. Setting the container as public would allow public access without requireing security checks

7. Associate a custom domain with the Azure Storage account through some CNAMEs.

8. Play around with it. Show it to your friends. Create other redirectors!

 

 

Want to learn more? Follow these links!

http://www.w3schools.com/js/js_window_location.asp

http://www.windowsazure.com/en-us/downloads

http://www.windowsazure.com/en-us/develop/net/how-to-guides/blob-storage

http://msdn.microsoft.com/en-us/library/windowsazure/ee395424.aspx

Recent Posts

  • Azure Resource Naming Convention

    All Azure users, especially after the Classic-to-ARM switchover, have noticed this... There are a lot of different resource types, even for a simple deployment. Naming them becomes critical to find the appropriate resource, especially in an emergency. To make matters worse, there are a lot of similar resource types which, if you're not careful, you may end up naming the same. Need a couple of new Virtual Machines? You have to name the availability group, storage accounts, load balancer, resouce …

    Read More...
  • The Global Azure Bootcamp 2015 - Malta!

    Malta is on the map once again for the Global Azure Bootcamp, a one-day deep-dive training into the Microsoft Azure cloud.

    From my last count, there will be 184 locations in total participating in this event around the world, from Hawaii to Japan! (Malta is bang in the middle!) And as usual, I'm again at it!

    The bootcamp will be on April 25th 2015 (hey that's tomorrow! I suck with keeping my blog updated!) and will be held at the Microsoft Innovation Centre at Skyparks, Luqa (just …

    Read More...
  • SEO Tip: How to block the *.azurewebsites.net domain

    Are you using Azure Websites to host your sites? Great! Then you know that whenever you deploy a website on Azure websites, you get the mysite.azurewebsites.net URL.

    The sub domain is great for you to test your application, but most probably you have your own domain that is mapped to the site. However, the azurewebsites.net domain is still there and still works! Do you know that this can be a problem?

    One of the rules in the SEO bible says that each page should only have one URL. Your …

    Read More...
  • Free eBook: Microsoft Azure Essentials - Fundamentals of Azure

    A great book on the fundamentals of Azure. If you're new to Azure, this is a great start. Get it now!

    The topics explored in this book include:

    Getting started with Azure: Understand what cloud computing is, visit the management portals, and learn about billing.

    Websites and Cloud Services: Learn about Azure Websites, from deployment to monitoring, and gain an understanding of the web and worker roles used in Azure Cloud Services.

    Virtual Machines: Explore the basic …

    Read More...
  • When Azure broke down...

    ... the world shook in terror on the 19th November 2014. 

    That means two things: firstly, the importance of a public cloud platform such as Azure.

    When such a huge thing gets a hiccup, a lot of people stress out. And they should. A lot of businesses nowadays depend on public cloud services (be it AWS or Azure), and each business trusts the availability of their service to these skilled engineers. It's true, Microsoft (or Amazon) do not own your availability promises. (You can check …

    Read More...
  • How to protect against POODLE attacks!

    Maybe you've heard about HEARTBLEED or SHELLSHOCK. POODLE is the next one.

    Anyone using Windows Server and IIS was lucky enough to avoid both HEARTBLEED (openssl library vulnerability - CVE-2014-0160) and SHELLSHOCK (unix bash shell vulnerability - CVE-2014-6271). However, POODLE hits everyone.

    What is POODLE?

    POODLE, spelled in caps to distinguish from our furry canine friends, stands for "Padding Oracle On Downgraded Legacy Encryption". It is a type of man-in-the-middle exploit …

    Read More...
  • Quick Tip: How to output custom error messages on Azure Web Sites

    You've published your latest masterpiece to Azure Web Sites. Great! How was it? Not bad!

    Now, does your application output its own error messages (HTTP 500, 404 etc?) IIS will by default hide your errors with the message:

    The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

    If you want to output your own custom output for non-200 statuses, you need to put this in your web.config:

      <system.webServer>

        < …

    Read More...
  • Azure icons in Visio

    Building any cloud apps lately? Great!

    Documenting them? Not so...

    Firstly, make sure you're using the best tools of the trade: Microsoft Visio. It's by far the easiest and most powerful tool for diagrams.

    Wanna add Azure-specific icons? Yeah, I know it's difficult to find them... But here's a secret: Visio supports SVG.

    What does that mean, you ask? Well, it seems that the current azure site uses SVG for the majority of icons. If you want to use a particular Azure icon, just find the …

    Read More...
  • How to migrate legacy apps to Microsoft Azure (part 1)

    How to migrate legacy apps to Windows Azure

    I see a lot of interest by startups to start using Windows Azure to power their products. It's the perfect platform to start small and have room to scale quickly and easily. But the most important advantage that startups have, is not having to worry about legacy issues. 

    A lot of companies would like to start using the cloud. By now, we know what the advantages are: scalability, resiliency, low management overhead, low service costs, etc. …

    Read More...
  • How to put a robots.txt file in your Windows Azure Blob Storage domain

    Google or Bing is indexing your public blobs on Windows Azure Blob Storage? Here is how to stop it...

    The idea is to put a robots.txt file at the root of the Azure Blob Storage domain. ie: http(s)://myaccount.blob.core.windows.net/robots.txt

    Follow these steps:

    1. Create a new container in your storage account in question. Name it $root and allow public access to blobs. This is a special container that serves as a default container for the storage account. Any blobs in this container may …

    Read More...