Mar 29 2016

Familiarization of Azure Storage Services Part 1

Windows Azure Overview

Most of the modern applications now uses the services of Azure Storage because it provides flexibility, scalability, durability, and huge availability of storage. This helps us develop large-scale applications. Azure Storage is accessible from anywhere in the world. It supports different applications built using a variety of languages. I am not going into the entire details of Azure Storage now. You can know more about Azure Storage Service from https://azure.microsoft.com/en-in/documentation/articles/storage-introduction/

Through this article I will try to explain the basic usage of Azure Storage Service. The first step in using this is to access the Azure Storage Service by creating a Storage Account. It gives access to the storage services. We can utilise features like BLOB, Table Storage, Queue and File in Azure.

BLOB Storage Service is for storing unstructured data like text or binary data such as media files, documents, images, etc.

Table Storage Service provides storage of structured data in the form of tables.

Queue Storage is used to transport messages between applications, provide reliable messaging for workflow processing, and for communication between components of cloud services.

File Storage Service allows file-sharing between cloud service applications on Azure virtual machines. It uses the Server Message Block (SMB) protocol for file sharing.

Now let me explain to you a sample application which uses all services of Azure Storage.

The above figure shows the functions of the Azure Storage Services. The document is uploaded into the BLOB storage and it returns the reference URL. User can access the document at this URL. Eg.

https://storagesample.blob.core.windows.net/containerName/document/mydocument.txt

In this application, this URL is stored into the Table Storage, Then the data is passed into the Queue so it can transport messages between components of cloud services. End user can access the data anywhere from the cloud service.

Step 1: To install Windows Azure Storage, run the following command in the Package Manager Console.

Install-Package WindowsAzure.Storage -Version 6.2.0

Step 2: We need to upload the document into the Blob, The figure represents blob service.

var storageAccount = CloudStorageAccount.Parse( 
CloudConfigurationManager.GetSetting("StorageConnectionString")); // reference to the connection string var blobClient = storageAccount.CreateCloudBlobClient(); // Create the blob client.
readonly static CloudBlobContainer Container = blobClient.GetContainerReference("DocumentFolder"); // Retrieve a reference to a container.
Container.CreateIfNotExists(); Container.SetPermissions(new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); // By default Blob is Private. Set the container to be public

Upload the document into blob and it will return the URL. Using this URL, the end user can access the document from the blob.

static Uri InsertToBlobFiles(Stream streamFile, string fileName) 
{ var blockBlob = Container.GetBlockBlobReference(fileName); blockBlob.UploadFromStream(streamFile); var blobRetrive = Container.GetBlockBlobReference(fileName); return blobRetrive.Uri; }

End user getting the blob files by

public static string GetBlobFiles(string blobUrl) 
{ string rawdata; var uri = new Uri(blobUrl); var rawdatafilepath =Path.GetFileName(uri.LocalPath); var blockBlob = Container.GetBlockBlobReference(blobUrl); using (var memoryStream = new MemoryStream()) { blockBlob.DownloadToStream(memoryStream); rawdata = System.Text.Encoding.UTF8.GetString(memoryStream.ToArray()); } return rawdata; }

You can delete the Blob simply by

// Retrieve reference to a blob 
var uri = new Uri(blobUrl);
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobUrl);
// Delete the blob. blockBlob.Delete();

In this architecture, the blob URL is stored in the Table Storage. So we should create a table structure using a custom class derived from TableEntity. To add an entity to a table, create a class that defines the properties of your entity. Row Key and Partition Key are user defined.

 public class RawData : TableEntity 
{ public RawData(string partitionKey, string rowKey) : base(partitionKey, rowKey) { } public RawData() { } public string Url{ get; set; } public Datetime TimeStamp { get; set; } }

We will now create the reference of the Azure Service and then the table using the following code.

private readonly CloudTable _tblRawData; 
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
CloudConfigurationManager.GetSetting("StorageConnectionString")); var tableClient = storageAccount.CreateCloudTableClient();
_tblRawData = tableClient.GetTableReference(“TableName”);

var uri = blobUrl;
_tblRawData.CreateIfNotExists(); var rowKey = Guid.NewGuid().ToString("N"); var partitionKey = string.Format(CultureInfo.InvariantCulture, "{0:MMddyyyy}", correctDate); var entityObj = new RawData(partitionKey, rowKey) { Url= uri, TimeStamp = DateTime.UtcNow, }; var insertOperation = TableOperation.Insert(entityObj); _tblRawData.Execute(insertOperation);

End user can access this cloud service and then access the Table Storage. Also, the user is allowed to get the blob storage. Access the Table data by using the rowKey and partitionKey. The code below shows how to get the table data.

var existsQuery = from e  
                in _tblRawData.CreateQuery()
                              where
                                  e.PartitionKey  partitionKey
                                  && e.RowKey  rowKey
                              select e;
                var data = existsQuery.FirstOrDefault();
 CloudTable table = tableClient.GetTableReference("TableName");

// Delete the table it if exists.
table.DeleteIfExists();

Summary

In this section, we covered BLOB Storage Service and Table Storage Service of Azure. We have seen how to upload, download, and delete blobs, how to store, delete, and get table of data in an Azure Table Service.

Well, that would be all in this article. Look out for my next article where I will explain the remaining two - Queue and File Service - of Azure Storage.

Loading Disqus Comments ...