Monday, 21 May 2012

What is Windows Azure Blob Storage


What is Windows Azure Blob Storage

Windows 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 100TB of blobs. 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-premise or Windows Azure-hosted service
You can use Blob storage to expose data publicly to the world or privately for internal application storage.

Concepts

The Blob service contains the following components:
Blob1
  • Storage Account: All access to Windows Azure Storage is done through a storage account. This is the highest level of the namespace for accessing blobs. An account can contain an unlimited number of containers, as long as their total size is under 100TB.
  • Container: A container provides a grouping of a set of blobs. All blobs must be in a container. An account can contain an unlimited number of containers. A container can store an unlimited number of blobs.
  • Blob: A file of any type and size. There are two types of blobs that can be stored in Windows Azure Storage: block and page blobs. Most files are block blobs. A single block blob can be up to 200GB in size. This tutorial uses block blobs. Page blobs, another blob type, can be up to 1TB in size, and are more efficient when ranges of bytes in a file are modified frequently. For more information about blobs, seeUnderstanding Block Blobs and Page Blobs.
  • URL format: Blobs are addressable using the following URL format:
    http://<storage account>.blob.core.windows.net/<container>/<blob>

    The following URL could be used to address one of the blobs in the diagram above:
    http://sally.blob.core.windows.net/movies/MOV1.AVI

Create a Windows Azure Storage Account

You need a Windows Azure storage account to use the blob storage service. You can manually create a storage account by following the below steps (you can also programmatically create a storage account using the REST API):
  1. In the navigation pane, click Hosted Services, Storage Accounts & CDN.
  2. At the top of the navigation pane, click Storage Accounts.
  3. On the ribbon, in the Storage group, click New Storage Account.
    Blob2

    The Create a New Storage Account dialog box will then open:
    Blob3
  4. In Choose a Subscription, select the account subscription that the storage account will be used with.
  5. In Enter a URL, type a subdomain name to use in the URI for the storage account. The entry can contain from 3-24 lowercase letters and numbers. This value becomes the host name within the URI that is used to address Blob, Queue, or Table resources for the subscription.
  6. Choose a region or an affinity group in which to locate the storage. If you will be using storage from your Windows Azure application, select the same region where you will deploy your application.
  7. Click OK.
  8. Click the View button in the right-hand column below to display and save the Primary access key for the storage account. You will need this in subsequent steps to access storage.
    Blob4

Setup a Windows Azure Storage Connection String

The Windows Azure .NET storage API supports using a storage connection string to configure endpoints and credentials for accessing storage services. You can put your storage connection string in a configuration file, rather than hard-coding it in code. In this guide, you will store your connection string using the Windows Azure service configuration system. This service configuration mechanism is unique to Windows Azure projects and enables you to dynamically change configuration settings from the Windows Azure Management Portal without redeploying your application.
To configure your connection string in the Windows Azure service configuration:
  1. Within the Solution Explorer of Visual Studio, in the Roles folder of your Windows Azure Deployment Project, right-click your web role or worker role and click Properties.
    Blob5
  2. Click the Settings tab and press the Add Setting button.
    Blob6
    A new Setting1 entry will then show up in the settings grid.
  3. In the Type drop-down of the new Setting1 entry, choose Connection String.
    Blob7
  4. Click the ... button at the right end of the Setting1 entry. The Storage Account Connection String dialog will open.
  5. Choose whether you want to target the storage emulator (the Windows Azure storage simulated on your local machine) or an actual storage account in the cloud. The code in this guide works with either option. Enter the Primary Access Key value copied from the earlier step in this tutorial if you wish to store blob data in the storage account we created earlier on Windows Azure.
    Blob8
  6. Change the entry Name from Setting1 to a "friendlier" name like StorageConnectionString. You will reference this connectionstring later in the code in this guide.
    Blob9
You are now ready to perform the How To's in this guide.

How to Programmatically access Blob Storage Using .NET

Add the following code namespace declarations to the top of any C# file in which you wish to programmatically access Windows Azure Storage:
using Microsoft.WindowsAzure;
using Microsoft.WindowsAzure.StorageClient;
You can use the CloudStorageAccount type and RoleEnvironment type to retrieve your storage connection-string and storage account information from the Windows Azure service configuration:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));
CloudBlobClient type allows you to retrieve objects that represent containers and blobs stored within the Blob Storage Service. The following code creates a CloudBlobClient object using the storage account object we retrieved above:
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

How to Create a 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:
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

// Create the blob client 
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve a reference to a container 
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Create the container if it doesn't already exist
container.CreateIfNotExist();
By default, the new container is private, so you must specify your storage account key (as you did above) to download blobs from this container. If you want to make the files within the container available to everyone, you can set the container to be public using the following code:
container.SetPermissions(
   new BlobContainerPermissions { PublicAccess = BlobContainerPublicAccessType.Blob }); 
Anyone on the Internet can see blobs in a public container, but you can modify or delete them only if you have the appropriate access key.

How to Upload a Blob into a Container

To upload a file to a blob, get a container reference and use it to get a blob reference. Once you have a blob reference, you can upload any stream of data to it by calling the UploadFromStream method on the blob reference. This operation will create the blob if it didn't exist, or overwrite it if it did. The below code sample shows this, and assumes that the container was already created.
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob"
CloudBlob blob = container.GetBlobReference("myblob");

// Create or overwrite the "myblob" blob with contents from a local file
using (var fileStream = System.IO.File.OpenRead(@"path\myfile"))
{
    blob.UploadFromStream(fileStream);
} 

How to List the Blobs in a Container

To list the blobs in a container, first get a container reference. You can then use the container's ListBlobs method to retrieve the blobs within it. The following code demonstrates how to retrieve and output the Uri of each blob in a container:
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Loop over blobs within the container and output the URI to each of them
foreach (var blobItem in container.ListBlobs())
{
    Console.WriteLine(blobItem.Uri);
} 
The blob service has the concept of directories within containers, as well. This is so that you can organize your blobs in a more folder-like structure. For example, you could have a container named 'photos', in which you might upload blobs named 'rootphoto1', '2010/photo1', '2010/photo2', and '2011/photo1'. This would virtually create the directories '2010' and '2011' within the 'photos' container. When you call ListBlobs on the 'photos' container, the collection returned will contain CloudBlobDirectory and CloudBlob objects representing the directories and blobs contained at the top level. In this case, directories '2010' and '2011', as well as photo 'rootphoto1' would be returned. Optionally, you can pass in a new BlobRequestOptions class with UseFlatBlobListing set to true. This would result in every blob being returned, regardless of directory. For more information, seeCloudBlobContainer.ListBlobs on MSDN.

How to Download Blobs

To download blobs, first retrieve a blob reference. The following example uses the DownloadToStream method to transfer the blob contents to a stream object that you can then persist to a local file. You could also call the blob's DownloadToFile, DownloadByteArray, or DownloadText methods.
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob"
CloudBlob blob = container.GetBlobReference("myblob");

// Save blob contents to disk
using (var fileStream = System.IO.File.OpenWrite(@"path\myfile"))
{
    blob.DownloadToStream(fileStream);
} 

How to Delete Blobs

Finally, to delete a blob, get a blob reference, and then call the Delete method on it.
// Retrieve storage account from connection-string
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
    RoleEnvironment.GetConfigurationSettingValue("StorageConnectionString"));

// Create the blob client
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();

// Retrieve reference to a previously created container
CloudBlobContainer container = blobClient.GetContainerReference("mycontainer");

// Retrieve reference to a blob named "myblob"
CloudBlob blob = container.GetBlobReference("myblob");

// Delete the blob
blob.Delete(); 

Post a Comment