- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 814字
- 2021-04-02 18:51:58
Creating and using a blob snapshot
The Windows Azure Blob Service supports the creation of read-only snapshots of a blob. A storage account is billed only for those blocks and pages in a snapshot that differ from those in the underlying blob. A blob snapshot is useful in providing a backup for a blob as it can be used to reset the blob to an earlier state. Indeed, multiple snapshots can be made over time allowing a historic record to be kept of changes to a blob.
An important use case for blob snapshots is provided by the Azure Drive feature. An Azure Drive is a page blob comprising an NTFS-formatted VHD that can be attached to a hosted service role instance and accessed as an NTFS drive. To avoid read-write conflicts, a single VHD page blob can be attached to only one instance at a time. However, a blob snapshot is read-only, allowing no possibility of write contention, so an arbitrary number of instances can attach a VHD snapshot simultaneously.
A blob snapshot is created using the CloudBlob.CreateSnapshot()
method. Each snapshot of a blob is distinguished by its creation time that must be provided to operations accessing the snapshot. A snapshot created at a particular datetime
is addressed by appending a snapshot={datetime}
query string to the URL identifying the underlying blob.
For example, the following is the complete URL for a snapshot taken on 11/25/2010
of a blob named SnapshotsExample
:
http://myaccountname.blob.core.windows.net/ chapter2/SnapshotsExample?snapshot=2010-11-25T02:02:40.1680568Z
The storage account is named myaccountname
and the container is named chapter2
.
In this recipe, we will learn how to create and use blob snapshots.
How to do it...
We are going to create a blob and create a snapshot of it. We are then going to download the snapshot. We do this as follows:
- Add a new class named
SnapshotsExample
to the project. - Set the Target Framework for the project to .NET Framework 4.
- Add the following assembly references to the project:
Microsoft.WindowsAzure.StorageClient System.Configuration
- Add the following
using
statements to the top of the class file:using Microsoft.WindowsAzure; using Microsoft.WindowsAzure.StorageClient; using System.Configuration; using System.IO;
- Add the following private member to the class:
private CloudBlobClient cloudBlobClient;
- Add the following constructor to the class:
public SnapshotsExample() { CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]); cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient(); }
- Add the following method, creating the container and a blob, to the class:
private void CreateContainerAndBlob(String containerName, String blobName) { CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName); cloudBlobContainer.CreateIfNotExist(); CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(blobName); cloudBlockBlob.UploadText("To be, or not to be"); }
- Add the following method, making a snapshot of the blob, to the class:
private DateTime MakeSnapshot(String containerName, String blobName) { CloudBlobContainer cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName); CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(blobName); CloudBlob snapshot = cloudBlockBlob.CreateSnapshot(); return (DateTime)snapshot.SnapshotTime; }
- Add the following method, retrieving the snapshot, to the class:
private void GetSnapshot(String containerName,String blobName, DateTime snapshotTime) { String uriFix = ""; if (cloudBlobClient.Credentials.AccountName =="devstoreaccount1") { uriFix = "/"; } String blobUri =String.Format("{0}{1}{2}/{3}?snapshot={4:O}",cloudBlobClient.BaseUri, uriFix, containerName, blobName,snapshotTime); CloudBlockBlob cloudBlockBlob =new CloudBlockBlob(blobUri, cloudBlobClient); using (MemoryStream memoryStream = new MemoryStream()) { cloudBlockBlob.DownloadToStream(memoryStream); memoryStream.Position = 0; using (StreamReader streamReader =new StreamReader(memoryStream)) { String blobText = streamReader.ReadToEnd(); } } }
- Add the following method, using the methods added earlier, to the class:
public static void UseSnapshotsExample() { String containerName = "{CONTAINER_NAME}"; String blobName = "{BLOB_NAME}"; SnapshotsExample example = new SnapshotsExample(); example.CreateContainerAndBlob(containerName, blobName); DateTime snapshotTime =example.MakeSnapshot(containerName, blobName); example.GetSnapshot(containerName, blobName, snapshotTime); }
- Add the following to the
configuration
section ofapp.config
:<appSettings> <add key="DataConnectionString"value="UseDevelopmentStorage=true"/> </appSettings>
How it works...
In steps 1 through 4, we set up the recipe. In step 5, we add a private member storing a CloudBlobClient
that we initialize in the constructor we add in step 6. This constructor retrieves the storage account information from the app.config
file. In step 7, we create a container and upload the blob to it.
In step 8, we invoke the CreateSnapshot()
method to make a snapshot of the blob. We return the SnapshotTime
because we need it later to access the snapshot. In step 9, we perform a fix to account for the different ways the Storage Client Library constructs endpoints for the storage service and the storage emulator. We then construct the full URI, including the snapshot
query parameter, and use it to download the snapshot using DownloadToStream()
.
In step 10, we invoke the methods added earlier to create a blob, make a snapshot, and retrieve it. We must replace {CONTAINER_NAME}
and {BLOB_NAME}
with the name of the container and blob we want to use.
In step 11, we add the connection string to the app.config
configuration file.
CloudDrive.Snapshot()
The CloudDrive
class has a Snapshot()
method which creates a snapshot of the VHD page blob backing the current CloudDrive
object. This method is needed because the Azure Drive simulation in the compute emulator is not integrated with the storage emulator. Consequently, neither VHD page blobs nor VHD snapshots are visible to the Azure Drive simulation. The specialized CloudDrive.Snapshot()
method can be used to create snapshots of Azure Drives in the compute emulator.
- 剪映短視頻制作全流程:剪輯、調色、字幕、音效
- AutoCAD 2018實用教程(第5版)
- Photoshop 2022從入門到精通
- Zenoss Core Network and System Monitoring
- 中文版CorelDRAW基礎培訓教程
- Vivado從此開始(進階篇)
- Photoshop網店美工實例教程(第2版 全彩微課版)
- ANSYS Workbench中文版超級學習手冊
- JBoss Tools 3 Developers Guide
- 中文版Photoshop CC基礎培訓教程
- Service Oriented Architecture with Java
- AutoCAD 2010 機械設計與制作技能基礎教程
- Procreate數字繪畫實戰教程(全彩微課版)
- WordPress Multisite Administration
- 音樂制作7天速成:Logic Pro編曲教程