Writes pages to a page blob. Namespace: Microsoft.WindowsAzure.StorageClient
Assembly: Microsoft.WindowsAzure.StorageClient (in Microsoft.WindowsAzure.StorageClient.dll)
Usage
Visual Basic |
---|
Dim instance As CloudPageBlob Dim pageData As Stream Dim startOffset As Long instance.WritePages(pageData, startOffset) |
Syntax
Visual Basic |
---|
Public Sub WritePages ( _ pageData As Stream, _ startOffset As Long _ ) |
C# |
---|
public void WritePages ( Stream pageData, long startOffset ) |
C++ |
---|
public: void WritePages ( Stream^ pageData, long long startOffset ) |
J# |
---|
JScript |
---|
Parameters
- pageData
Type: System.IO.Stream
A stream providing the page data.
- startOffset
Type: System.Int64
The offset at which to begin writing, in bytes. The offset must be a multiple of 512.
Example
The following code example creates a page blob, writes some pages to it, and prints out the page ranges to the console.
C# | Copy Code |
---|---|
static void WriteToPageBlob(Uri blobEndpoint, string accountName, string accountKey) { //Create service client for credentialed access to the Blob service. CloudBlobClient blobClient = new CloudBlobClient(blobEndpoint, new StorageCredentialsAccountAndKey(accountName, accountKey)); // create container if it does not exist CloudBlobContainer cloudContainer = blobClient.GetContainerReference("mypageblobs"); cloudContainer.CreateIfNotExist(); //Get a reference to the page blob that will be created. CloudPageBlob pageBlob = cloudContainer.GetPageBlobReference("apageblob"); //Generate some data to write. byte[] data = new byte[1024]; Random rnd = new Random(); rnd.NextBytes(data); //Create a 100 MB page blob. pageBlob.Create(100 * 1024 * 1024); //Write two sets of pages. Note that you can write 4 MB per call to WritePages(). pageBlob.WritePages(new MemoryStream(data), 0); pageBlob.WritePages(new MemoryStream(data), 4096); //Populate the page blob's attributes. pageBlob.FetchAttributes(); Console.WriteLine("Blob length = {0}", pageBlob.Properties.Length); //Print out the current range of pages. PrintPageRanges("Before write to 10240:", pageBlob); //Write another page. pageBlob.WritePages(new MemoryStream(data), 10240); //Print out the new range of pages. PrintPageRanges("After write to 10240:", pageBlob); //Clear a page. pageBlob.ClearPages(4096, 1024); //Print out the new range of pages. PrintPageRanges("After clearing page at 4096:", pageBlob); //Delete the page blob. pageBlob.Delete(); } static void PrintPageRanges(string msg, CloudPageBlob cloudBlob) { //Write out the page ranges for the page blob. IEnumerable<PageRange> ranges = cloudBlob.GetPageRanges(); Console.Write("{0}:<", msg); foreach (PageRange range in ranges) { Console.Write(" [{0}-{1}] ", range.StartOffset, range.EndOffset); } Console.WriteLine(">"); } |
Remarks
The WritePages method writes a range of pages to a page blob. This method can only be called on an existing page blob; it cannot be called to create a new page blob.
Each range of pages submitted with WritePages may be up to 4 MB in size. The start and end range of the page must be aligned with 512-byte boundaries.
The Blob service serializes concurrent writes to overlapping pages sequentially: the last page processed by the service determines the blob's content. Therefore, to ensure the integrity of the blob's content, you should handle writes to overlapping pages using one or more of the following approaches:
You can check the value of the blob's LastModifiedUtc property for each successful call to WritePages. The order of responses returned from the Blob service does not necessarily correspond to the order in which they were executed by the service. But the last modified time always indicates the order in which the service processed the requests.
You can perform updates conditionally based on the blob's ETag or last modified time using optimistic concurrency. This approach works well if the number of concurrent writes is relatively low. Specify an access condition using the AccessCondition property of the BlobRequestOptions class.
You can acquire a least on the blob to lock it against other writes. See the Lease method of the BlobRequest class.