官术网_书友最值得收藏!

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:

  1. Add a new class named SnapshotsExample to the project.
  2. Set the Target Framework for the project to .NET Framework 4.
  3. Add the following assembly references to the project:
    Microsoft.WindowsAzure.StorageClient
    System.Configuration
  4. 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;
  5. Add the following private member to the class:
    private CloudBlobClient cloudBlobClient;
  6. Add the following constructor to the class:
    public SnapshotsExample()
    {
      CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(ConfigurationManager.AppSettings["DataConnectionString"]);
      cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient();
    }
  7. 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");
    }
  8. 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;
    }
  9. 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();
        }
      }
    }
  10. 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);
    }
  11. Add the following to the configuration section of app.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.

主站蜘蛛池模板: 淳安县| 于都县| 和静县| 南开区| 苗栗市| 宜黄县| 台中县| 兴城市| 合江县| 肃宁县| 漯河市| 象州县| 海兴县| 金山区| 巴中市| 穆棱市| 琼海市| 荣昌县| 西畴县| 台南市| 霍州市| 阳谷县| 松潘县| 十堰市| 菏泽市| 余姚市| 汨罗市| 洛南县| 青神县| 兴文县| 宁远县| 洪洞县| 会泽县| 名山县| 廊坊市| 贡觉县| 宁陵县| 无极县| 嘉黎县| 肥东县| 乌鲁木齐县|