October 20, 2014
Hot Topics:
RSS RSS feed Download our iPhone app

Microsoft Azure Storage with .NET from 0 to 60

  • September 10, 2012
  • By Jeffrey Juday
  • Send Email »
  • More Articles »

.NET developers cannot ignore Microsoft Azure.  June 2012 changes brought Virtual Machines, Virtual Networks, performance upgrades, and significant cost reductions.  As more Azure features unfold, some developers will soon be forced to justify "why not Azure" rather than "why Azure".  Clearly .NET developers need to get acquainted with Azure.  A simple way to get acquainted with Azure is to setup a free trial and try the Azure storage features.

Azure storage is one of the Azure cornerstones.  Much in Azure starts with storage.  Azure logging data goes into storage.  Compute images are stored in storage.  Azure drives are built on storage. 

Blob storage is one of the storage options.  Blob storage encapsulates concepts common to all the storage options; so it makes a good place to start.

Getting Started

Some Azure access comes bundled with an MSDN subscription.  MSDN subscribers could start there.  This article approaches Azure with the Trial version.  The Azure Trial link is everywhere in the Azure documentation.  As of writing this article; there was a trial link here http://www.windowsazure.com/en-us/ .  An Azure trial will be enough to give a developer a sense of what Azure is and how to work with it. 

Setting up the Trial version requires an email address, Windows Live Id, a phone number, and a Credit card.  Azure communication is done through email.  The Windows Live Id is for authentication with the Admin portal.  "Keyword" setup information is passed via the phone number or text option to a wireless phone.

I originally balked at the credit card information and reviewed why the credit card was needed.  The credit card is part of the identification process; nothing more.  I have been monitoring my credit card transactions and a month after creating the Azure account this appears to be true. 

After walking through the setup; Azure sends a series of emails directing a developer to the Admin Portal.  As of writing this article; there were two portals, a newer beta/CTP portal and the prior version portal.  The process funnels a new user into the newer portal; but some features, like Azure Service Bus Administration are not accessible in the newer Portal.  An Administrator can move between Portal versions through a "Preview" link at the top of the newer Portal page.

Accessing the old PortalAccessing the old Portal
Figure: 1 Accessing the old Portal

After creating an Azure account and setting up the Trial a developer must create a storage account to access Azure storage.  Storage accounts are setup from the Azure Portal.

Creating a Storage Account

All Azure storage mechanisms require a storage account.  A storage account must be lowercase and unique across Azure storage.  Various mechanisms are in place to verify all this for you.  Creating a Storage Account takes Azure 8 minutes or so as Azure allocates storage resources.

One interesting aspect with Cloud offerings like Azure is: decisions normally made by an Infrastructure architect or IT Professional are now accessible to a developer.  The downside to this is that developers may need to understand how these new capabilities work.  For example; developers should consider Physics when defining an Azure storage Datacenter.  Accessing storage from an account on another continent from a developer’s location will be slower; though for trial purposes performance differences may be imperceptible. 

By default, Azure storage is Geo redundant; data placed in storage is replicated to a data center 400+ miles away.  Though this may not be important to running the trial; disaster recovery should be considered if a developer moves onto building a production application.

Now that storage is provisioned; attention moves to configuring the development environment.

Configuring for Development

Azure supports various development platforms.  An overview of all platforms is beyond the scope of this article.  Code samples were created with the Azure .NET June 2012 SDK, .NET 4.0, and Visual Studio 2010.

The June 2012 Azure SDK can be found here http://www.microsoft.com/en-us/download/details.aspx?id=29988 .  Installation instructions can be found towards the bottom of the SDK download site.  I stopped installing components after the .NET Azure Library installation step.

Once the library and its dependencies are installed; a developer should create a Console Application and add the Azure Library references. 

After creating a Console Project; change the Console Application properties to the full .NET Framework.  By default, a Console Application is configured with the .NET Client Profile.  To change the Console Application target Framework from Client Profile, access Properties on the project, select the Build tab, and change from Client Profile to .NET Framework.

Changing to .NET Framework from Client Profile
Figure 2: Changing to .NET Framework from Client Profile

The Library references appear in the following diagram.  Populating references happens asynchronously.  The Azure components will be populated towards the bottom unless a developer waits for the process to complete before hunting for components.

Library References
Figure 3: Library References

With a new project and installed components the development environment is ready.

Creating a Container and a Blob

Programmatically accessing storage requires an AccountName and AccountKey.  The AccountName is the name entered when the storage account is created.   AccountKey comes from the "Account Keys" link on the bottom toolbar in the following location.

AccountKey location
Figure 4: AccountKey location

Blobs are contained in a Container.  So before creating a Blob a developer must create a Container.  Developers create and delete Containers and Blobs through a CloudBlobClient class.  The following code opens storage and creates a CloudBlobClient.

using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
using System.IO;
 
namespace BlobTest
{
    class Program
    {
        static void Main(string[] args)
        {
var acct = "DefaultEndpointsProtocol=https;AccountName=youraccountnamehere;AccountKey=PPVAtuxbxK8QusWzaccountkeyhidemeooosOrAf3Xal=====8LA6l9XJyV/Dgy+FGEPgiCT8oKzq7M7k+ry4g==";
 
var cloudAcct = CloudStorageAccount.Parse(acct);
var client = cloudAcct.CreateCloudBlobClient();
 

The code uses a ConnectString.  Notice how the DefaultEndpointProtocal utilizes HTTPs (HTTP with SSL). Plain HTTP is also available; however, plain HTTP is more unsecure. 

The .NET library sends HTTP: PUT, DELETE, and GET messages to Azure.  Messages can be viewed in tools like Fiddler.  The Azure library uses standard ports like 80 (HTTP) and 443 (HTTP with SSL).

Typically a developer would store the ConnectString in the App.Config (Web.config) file.  ConnectStrings should be encrypted.  Encryption details can be found among the following ConnectString storage guidelines http://msdn.microsoft.com/en-us/library/89211k9b(v=VS.80).aspx .

As stated earlier in the article, Blobs and Containers are created through the CloudBlobClient class.  Container names are limited to lowercase text (among other constraints).  A complete overview of Container naming constraints is beyond the scope of this article, but can be found in the resources at the end of the article.  The following code creates a container and adds a file from the local hard drive to Blob storage.

var container = client.GetContainerReference("test");
container.CreateIfNotExist();
 
var blob = container.GetBlobReference("testblob");
 
blob.Metadata.Add("RealName", "TextFile1.txt");
 
using (var source = new FileStream("TextFile1.txt",FileMode.Open,FileAccess.Read))
{               
    blob.DeleteIfExists();
    blob.UploadFromStream(source);
}
 

Azure .NET methods work with the System.IO Stream based classes.  The code also demonstrates adding Metadata to the Blob.  Metadata is helpful if, for example, a developer wants to store the original file name with the Blob.

Conclusion

This was a quick introduction to Azure Administration and .NET development.   The introduction took a developer from creating an Azure Trial Account thru storing a file in Azure Storage using the .NET library.  Developers looking for more information should start with the Resources following the article.

Resources

Azure Home Site

Azure Blog

June 2012 New Capabilities

June 2012 SDK

Azure Tech Ed. Videos

Azure Tech Ed. Videos

Azure Internals


Tags: .NET, Cloud, Microsoft, Azure, Storage, MICROSOFT AZURE PLATFORM




Comment and Contribute

 


(Maximum characters: 1200). You have characters left.

 

 


Sitemap | Contact Us

Rocket Fuel