Share via


Azure Getting Started - Basic Operations on Blob Storage

Introduction

This sample explains how to use Azure Blob Storage (Upload, Download, List and Delete methods were implemented).

What is Blob Storage

According to the Azure Website, Azure Blob Storage is " ... a service for storing large amounts of unstructured data that can be accessed from anywhere in the world via HTTP or HTTPS. A single blob can be hundreds of gigabytes in size, and a single storage account can contain up to 200TB of blobs if it was created with the latest version".

"Common uses of Blob storage include:

  • Serving images or documents directly to a browser
  • Storing files for distributed access
  • Streaming video and audio
  • Performing secure backup and disaster recovery
  • Storing data for analysis by an on-premises or Azure-hosted service

You can use Blob storage to expose data publicly to the world or privately for internal application storage." [windowsazure.com]

"In the most general sense, the term 'blob' is commonly understood to mean 'Binary Large Object'. Many of us are familiar with this term from its usage in database-land, where 'blob data' might be data stored in our database." [codeproject.com]

Azure have extended this notion to *"generic storage implementations which allow client data of any sort to be uploaded/persisted on the vendor storage server in binary format. (...) Windows Azure persists binary data ('Blobs') in 'Containers'." *[codeproject.com]

Azure Blob Storage Model

http://i1.code.msdn.s-msft.com/how-to-use-azure-blob-16882fe2/image/file/108094/1/azuremodel.png

Install NuGet (Azure Storage)

"This is quite straightforward in the Azure portal. Just create a storage account. You do need to provide an account name. Each storage account can have many 'containers' so you can share the same storage account between several sites if you want." [Mark heath]

"To install Windows Azure Storage, run the following command in the Package Manager Console

PM> Install-Package WindowsAzure.Storage
"
[nuget.org]

Create Container

"All storage blobs reside in a container. You can use a CloudBlobClient object to get a reference to the container you want to use. You can create the container if it doesn't exist." [windowsazure.com]

Implement of Crud Methods

  • Upload Data:
             private static  void InserData(CloudBlobContainer container)  
         { 
             try
             { 
                 // Retrieve reference to a blob 
                 CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob"); 
  
                 using (var fileStream = System.IO.File.OpenRead(@"localFile")) 
                 { 
                     blockBlob.UploadFromStream(fileStream); 
                 } 
             } 
             catch
             { 
                 throw; 
             } 
         }
  • Download Data:
             private static  void GetData(CloudBlobContainer container)  
        { 
            try
            { 
                // Retrieve reference to a blob 
                CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob"); 
 
                using (var fileStream = System.IO.File.OpenWrite("localFile")) 
                { 
                    blockBlob.DownloadToStream(fileStream); 
                } 
            } 
            catch
            { 
                throw; 
            } 
        }

**
**

  • List Data:
               private static  void ListData(CloudBlobContainer container)  
         { 
             try
             { 
                 // 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); 
  
                     } 
                     else if  (item.GetType() == typeof(CloudPageBlob)) 
                     { 
                         CloudPageBlob pageBlob = (CloudPageBlob)item; 
  
                         Console.WriteLine("Page blob of length {0}: {1}", pageBlob.Properties.Length, pageBlob.Uri); 
  
                     } 
                     else if  (item.GetType() == typeof(CloudBlobDirectory)) 
                     { 
                         CloudBlobDirectory directory = (CloudBlobDirectory)item; 
  
                         Console.WriteLine("Directory: {0}", directory.Uri); 
                     } 
                 } 
             } 
             catch
             { 
                 throw; 
             } 
         }
  • Delete Data:
              private static  void DeleteData(CloudBlobContainer container)  
         { 
             try
             { 
                 // Retrieve reference to a blob 
                 CloudBlockBlob blockBlob = container.GetBlockBlobReference("blob"); 
  
                 blockBlob.Delete(); 
             } 
             catch
             { 
                 throw; 
             } 
         }

Azure Blob Storage Resources

Some good online resources about Windows Azure Storage Resources:

Code Samples

All of this sample can be found and downloaded in Microsoft Code Gallery: