Click here to Skip to main content
Click here to Skip to main content
Articles » Cloud Computing » Azure » General » Revisions
 

Tagged as

Understanding Block Blobs

, 21 Apr 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an old version of the currently published tip/trick.

Introduction

The blob storage service provided 2 falvours of blob storage.

  1. Block blob
  2. Page blob

In this article I would like to focus on Block blob for its wide use.
I'll go over the block blob concept following with some exampled for clarification. 

Background 

Block vs Page Blob 

Block blob provide the ability to upload large amount of data very fast. Why ?
Since the upload itself can be done in parallel and the size of each block can verified (up to 4MB).
In contrast the Page blob is best optimized  for random accessed. Meaning I can specify an offset in the blob for reading/writing.
Each page in the page blob has fixed size of 512KB (so the storage service can optimize the access internally)

Block Blob Concepts

If you go over the Storage Service REST API's or the Microsoft.WindowsAzure.StorageClient namespace you probably noticed that there
isn't any way to delete a single block. Why ?
To answer this question lets first go over some concept:

The blocks in the storage service are organized into 2 lists

  1. Committed
  2. Uncommitted

Lets look from the Azure Storage Service perspective:

Any time you upload a block (calling PutBlock) to the storage service it goes directly to the Uncommitted block list.
Blocks on the Uncommitted list are saved in the storage service up to 1 week  if they are not committed (participating in the Committed list).
In order to commit the block one should call PutBlockList and specify the block ids.

So how do I delete a block ?
By simply not mentioning the block id to be deleted in the list when calling PutBlockList.

How do i update a block content ?

Using the same block id concept done for deleting. You upload a new block that has the same id of an old one (the blob service will take the latest block) 

Using the code

The following simple example demonstrate how to delete/update blocks from a blob.
I used the Microsoft.WindowsAzure.StorageClient api's 

const string storageName = "[Put_Storage__Name]";
const string storageKey = "[Put_Private_Key]";
CloudStorageAccount storage = CloudStorageAccount.Parse(String.Format("DefaultEndpointsProtocol=https;AccountName={0};AccountKey={1}", storageName, storageKey));
CloudBlobClient cloudBlobClient = storage.CreateCloudBlobClient();

CloudBlockBlob blob = cloudBlobClient.GetBlockBlobReference("mycontainer/testfile.txt");

// Get committed blocks
List<string> commitedBlocks = new List<string>();
// Grab the first 100 blocks ids
commitedBlocks.AddRange(blob.DownloadBlockList(BlockListingFilter.Committed).Take(100).Select(id => id.Name));
// Noticed tha blocks not in the list will be garbage collected and deleted by the storage service
blob.PutBlockList(commitedBlocks);

// Update the first block
const String newBlockContent = "testblock";
var blockID = Convert.ToBase64String(BitConverter.GetBytes(0));
// Upload the content
blob.PutBlock(blockID, new MemoryStream(Encoding.Default.GetBytes(newBlockContent)), null);
// Commit the blocks
blob.PutBlockList(commitedBlocks);  

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Shay Yannay
Software Developer Quest Software
Israel Israel
Shay Yannay is a Senior Software Developer and Windows Azure Domain Expert.
He is experienced with designing and developing highly scalable, distributed, 24x7 availability complex system. Shay also specializes in performance management & diagnostics of multi-tier applications.
He is passionate about the cloud technologies and trends, specifically with Microsoft Azure.
He currently works for Dell as an Azure Specialist.
 
Shay holds a B.Sc in Communication Systems Engineering from the Ben-Gurion university.
 
Personal Blog: http://shayyannay.wordpress.com
Follow on   Twitter

Comments and Discussions

Discussions on this specific version of this article. Add your comments on how to improve this article here. These comments will not be visible on the final published version of this article.
 
QuestionIt needs a little work on the formatting PinmvpOriginalGriff21-Apr-12 22:12 
AnswerRe: It needs a little work on the formatting PinmemberShay Yannay21-Apr-12 22:42 
GeneralRe: It needs a little work on the formatting PinmvpOriginalGriff21-Apr-12 22:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.


Discussions posted for the Published version of this article. Posting a message here will take you to the publicly available article in order to continue your conversation in public.
 
-- There are no messages in this forum --
| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.1411023.1 | Last Updated 22 Apr 2012
Article Copyright 2012 by Shay Yannay
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid