- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 1309字
- 2021-04-02 18:51:58
Uploading a VHD into a page blob
An instance of a Windows Azure role comprises several virtual disks (VHD) deployed into a virtual machine. One VHD contains the Guest OS; another contains the role package, while the third is a writable VHD containing the local resources of the instance.
Windows Azure provides the Azure Drive feature whereby an arbitrary VHD, contained in a page blob, can be attached to an instance and then accessed as an NTFS drive. This VHD must be an NTFS-formatted, fixed-size VHD. The VHD can be created directly as a page blob or uploaded like any other page blob.
The Disk Management snap-in for the Microsoft Management Console (MMC) can be used to create and format a VHD on a local system. Once attached to the local system, files can be copied to, or created on the file system of the VHD just as they can with any hard drive. The VHD can then be detached from the local system and uploaded as a page blob to the Windows Azure Blob Service.
A page blob, which may have a maximum size of 1 TB, comprises a sequence of 512-byte pages. All writes to a page blob must be aligned with a page boundary. There is no charge for an empty page in a page blob. There is no need to upload empty pages in the VHD, which means that bandwidth need not be wasted uploading empty pages. The Blob service identifies these empty pages when a download of one of them is requested and it merely injects a page full of 0x0 bytes into the response stream.
Uploads to block blobs comprise a two-step process: upload the blocks and commit the blocks to the blob. Uploading a page blob is a single step process: write one or more 512-byte pages to the blob. Once uploaded, these pages immediately form part of the blob. This can cause problems if multiple writers are writing to the page blob simultaneously. The Windows Azure Storage Service REST API provides support for a sequence number that can be used to control which writer wins a contest to write to a single page. This functionality is not exposed in the Windows Azure Storage Client library.
In the Storage Client library, the CloudPageBlob
class provides various methods supporting page blob uploads. CloudPageBlob
is derived from CloudBlob
. Note that the upload methods in CloudBlob
class are not supported for CloudPageBlob
. Instead, CloudPageBlob
provides synchronous and asynchronous WritePages()
methods specifically for uploading page blobs. Additionally, there are synchronous and asynchronous forms of the GetPageRanges()
method that returns an IEnumerable<PageRange>
of the non-empty page ranges in the blob.
In this recipe, we will learn how to upload a VHD into a page blob.
Getting ready
We use the disk management snap-in to create and format a VHD on the local system that we will upload with the recipe. There is more documentation on the disk management snap-in at http://technet.microsoft.com/en-us/library/dd979539(WS.10).aspx.
We launch disk management by clicking on Start, typing diskmgmt.msc
in the Search box, and then pressing Enter. The Create VHD and Attach VHD operations are on the Actions menu.
We use the disk management snap-in for the following tasks:
- Creating VHD—as "fixed size" with a specified size in the specified location
- Initializing disk with a master boot record
- Creating a "new simple volume" formatted with NTFS with a specified drive letter
- Copying files and directories to the specified drive
- Detaching VHD
If the VHD already exists, then we use the disk management snap-in for the following tasks:
- Attaching the VHD
- Copying files and directories to the specified drive
- Detaching VHD
How it works...
In steps 1 through 4, we set up the class. In step 5, we define various constants and the CloudBlobClient
we use to access the Blob service. We initialize this in the constructor we add in step 6.
In step 7, we add the method that controls the upload process. We open a FileStream
on the VHD and create a page blob the same size as the VHD. We then iterate over the VHD file in increments of 1 MB, invoking UploadPages()
to upload the data in each 1 MB window. Finally, we invoke UploadFooter()
to upload the VHD footer.
In step 8, we add the UploadPages()
method to upload non-empty pages to the page blob. We read the VHD into a buffer in 1 MB chunks. We then look for ranges of 512-byte pages containing some data and upload only these ranges. Remember, we do not need to upload empty pages. The tricky part of this process is identifying the end of a range when we come across an empty page or reach the end of the buffer.
In step 9, we add the UploadFooter()
method to upload the 512-byte VHD footer. We simply load the footer into a Byte[]
buffer and create a MemoryStream
on it. We then invoke WritePages()
to upload this to the page blob.
In Step 10, we add a method that returns false
if every byte of a specified 512-byte page of a buffer is 0x0; otherwise, NextPageHasData()
returns true
.
In step 11, we add a method to use the methods added earlier. We must replace {CONTAINER_NAME}
and {BLOB_KEY}
with actual values for account name and access key. We must also replace {PATH_TO_THE_VHD}
with the actual path to a VHD.
In step 12, we add the connection string to the app.config
configuration file. We must replace {ACCOUNT_NAME}
and {ACCOUNT_KEY}
with actual values for the account name and access key.
There's more...
When uploading an arbitrary file that—unlike the VHD—is not an integral multiple of the upload buffer, we need to handle correctly the final chunk that is smaller than the size of the upload buffer.
See also
- See the Uploading blocks to a block blob recipe to see how to upload a block blob.
- 數(shù)碼攝影后期零基礎入門教程
- AutoCAD 2018實用教程(第5版)
- 零基礎玩轉(zhuǎn)AI繪畫
- ASP.NET jQuery Cookbook
- OpenCart 1.4 Template Design Cookbook
- 網(wǎng)店美工設計:Photoshop CC案例教程(視頻指導版)
- Origin 2022科學繪圖與數(shù)據(jù)分析(高級應用篇)
- Mastercam 2019 完全自學寶典
- Photoshop CS6淘寶美工完全實例教程(培訓教材版)
- 平面設計師實戰(zhàn)教程
- Photoshop攝影后期必修5項核心技法 基本調(diào)整+光影校正+色彩修飾+局部處理+銳化降噪
- Sage ACT! 2011 Dashboard and Report Cookbook
- 早該這樣學!Photoshop比你想的簡單
- 中文版SketchUp/Artlantis印象渲染技術精粹
- AutoCAD計算機輔助設計標準教程(慕課版)