Azure Storage - 26: Azure Blob Service - Page Blobs


In the previous post I have showed how to work with block blobs, in this post I am going to provide more info about page blobs.


Refer to previous posts to get started with Azure Blob Service.


Page blobs are optimized for random read and write operations. The following code creates a pages blob and writes some data in it:

var pages = container.GetPageBlobReference("pages1.dat");
using (var stream = pages.OpenWrite(512))
    stream.Write(new byte[512], 0, 512);

In case you try to write more data than allocated for blob, you will get the following exception:

The remote server returned an error: (416) The page range specified is invalid.

It is possible to read data from a pages blob using OpenRead method:

var pages = container.GetPageBlobReference("pages1.dat");
var buffer = new byte[10];
using (var stream = pages.OpenRead())
    stream.Seek(33, SeekOrigin.Begin);
    stream.Read(buffer, 0, 10);

This code will read 10 bytes from a random position in the stream. It is fully supported by page blobs.

You can work with individual pages like this:

var pages = container.GetPageBlobReference("pages.dat");
pages.WritePages(new MemoryStream(new byte[512]), 0);

This code will create a new page blob and fill a single page with zeroes.

You can get a list of used pages. Consider a following example:

var pages2 = container.GetPageBlobReference("pages2.dat");
pages2.Create(1024 * 1024 * 1024);
var ranges1 = pages2.GetPageRanges();
Console.WriteLine($"Count Initial: {ranges1.Count()}");

pages2.WritePages(new MemoryStream(new byte[1024]), 512 * 10);
pages2.WritePages(new MemoryStream(new byte[512]), 512 * 20);
pages2.WritePages(new MemoryStream(new byte[512]), 512 * 20);
var ranges2 = pages2.GetPageRanges();

Console.WriteLine($"Count after 3 writes: {ranges2.Count()}");
foreach (var range in ranges2)

pages2.ClearPages(512 * 20, 512);

var ranges3 = pages2.GetPageRanges();
Console.WriteLine($"Count after clear: {ranges3.Count()}");
foreach (var range in ranges3)

This code outputs the following lines:

Count Initial: 0
Count after 3 writes: 2
Count after clear: 1

GetPageRanges method returns a list of used pages and a range of each page. ClearPages method clears a page. The code writes a couple pages, clears a single page and outputs resulting pages range.


To get more information about page blobs go to About Page Blobs and (OUTDATED) Using Windows Azure Page Blobs and How to Efficiently Upload and Download Page Blobs