Azure

Azure : Blob Storage / Retrieval

This is a new post in a series of beginners articles on how to do things in Azure. This series will be for absolute beginners, and if you are not one of those this will not be for you.

You can find the complete set of posts that make us this series here :

https://sachabarbs.wordpress.com/azure/

This time we will look at how to use Azure blob storage for uploading things like files (images, word documents, whatever you like really).

Introduction To Blob Storage

What exactly is Blob Storage? Well it is actually very simple, it is Azure hosted storage that allows you to upload large amounts of unstructured data (typically binary data, so bytes) that may be shared publicly using http/https.

Typical usage may be:

  • Storing images
  • Storing documents
  • Storing videos
  • Storing music

A typical Azure blob service would make use of these components

image

Account

This would be your Azure storage account. You must create a storage account using the Azure portal, which you would do through the portal : https://manage.windowsazure.com (we will see more on this in just a minute)

Container(s)

These belong to Account, and are using to group blob(s). Each account can have unlimited number of containers, and each container may contain an unlimited number of blobs.

Blob(s)

Blobs represent the Azure hosted data that represent the originally uploaded binary data. It is the blobs that you would eventual end up loading when you share an Azure blob storage url.

There are 2 types of blob storage available

Block Blobs

These are binary blocks of up to 200GB, where you can upload 64MB at one time, Typically for a larger block, you would spit things up and upload them in chunks using multiple threads, and Azure will reassemble them making the available as a single blob.

Page Blobs

These can be up to 1TB and consist of a collection of 512 pages. You would set a maximum size when creating the page blob. I have not used these so much, and personally I think these are here to support other Azure features like Virtual Hard Drives (VHDs) which are stored as page blobs in Azure Storage.

 

Url Syntax

The actual blob url format is as shown below:

http://<storage account>.blob.core.windows.net/<container>/<blob>

 

How To Get An Azure Storage Account

The first thing you will need to do is create a storage account, this is easily achieved by using the portal. Go to the portal : https://manage.windowsazure.com, and then click new

image

Then pick storage, and go through creating a new Storage Account

image

 

Then once you have that you can open the newly created storage account, and click the dashboard, and that will show you the relevant connection strings which you may use in your application

image

Using The Storage Emulator

NOTE : If you just want to try things out without going through the process of creating a Storage Account, you can actually use the Storage Emulator, which you can do using a account connection string something like this:

<!-- TODO : This would need to change to live azure value when deployed -->
    <add key="azureStorageConnectionString" value="UseDevelopmentStorage=true;" />

Since I am writing the bulk of these posts on a train without any internet connectivity, I will be using the storage emulator in any demos in this post.

If you want to use the emulator, ensure that is running, and that you have enabled the storage emulator

image

image

 

Getting The Right NuGet Package

You will need to install this NuGet Package to work with Azure Blob Storage “WindowsAzure.Storage”

 

Creating A Container

This is done using the following sort of code

CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    ConfigurationManager.AppSettings["StorageConnectionString"]);

CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// get blob container
CloudBlobContainer container = blobClient.GetContainerReference("testcontainer5");
container.CreateIfNotExists();


container.SetPermissions(
new BlobContainerPermissions
{
    PublicAccess = BlobContainerPublicAccessType.Blob
});

Uploading A File To Azure Blob Storage

This is done using the following code

CloudBlockBlob cloudBlockBlob = container.GetBlockBlobReference(
    "mandrillBlobUploaedToAzure.jpg");
cloudBlockBlob.Metadata["TypeOfImage"] = "Animal";

// Create or overwrite the "myblob" blob with contents from a local file.
using (var fileStream = System.IO.File.OpenRead(@"C:\Users\User\Pictures\mandrill.jpg"))
{
    cloudBlockBlob.UploadFromStream(fileStream);
    Console.WriteLine("Blob Url : {0}", cloudBlockBlob.Uri);
}

 

Listing All Blobs In A Container

This is done using the following code

container = blobClient.GetContainerReference("sachasContainer");

// Loop over items within the container and output the length and URI.
foreach (IListBlobItem item in container.ListBlobs(null, false))
{
    if (item.GetType() == typeof(CloudBlockBlob))
    {
        CloudBlockBlob blob = (CloudBlockBlob)item;

        Console.WriteLine("Block blob of length {0}: {1}", blob.Properties.Length, blob.Uri);

    }
}

Download Blobs

This is done using this sort of code

CloudBlockBlob blockBlob = container.GetBlockBlobReference("mandrillBlobUploaedToAzure.jpg");

// Save blob contents to a file.
using (var fileStream = System.IO.File.OpenWrite(@"C:\Users\User\Pictures\XXX.jpg"))
{
    blockBlob.DownloadToStream(fileStream);
}

Blob Metadata

Blobs support metadata via a dictionary which is available using the Metadata property which is a simple key/value pair.

 

How To Add Descriptive Metadata To A Blob

This is actually very easy, you just store more data in either a SQL Azure or SQL Azure table storage where you would include the blobs Url.  Job done

 

Deleting A Blob From A Container

This is done using this sort of code

blockBlob = container.GetBlockBlobReference("mandrillBlobUploaedToAzure.jpg");

// Delete the blob.
blockBlob.Delete();

Leave a comment