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

Using blob directories

The Windows Azure Blob Service uses a simple organizational structure for containers and blobs. A storage account has zero or more containers each of which contains zero or more blobs. Containers contain only blobs and may not contain other containers. There is no hierarchy for containers.

The Windows Azure Storage Service REST API provides support for a simulation of a hierarchical directory structure through an ability to parse blob names containing a special delimiter character and navigate the list of blobs while taking account of that delimiter. This delimiter is the forward-slash symbol (/). The Windows Azure Storage Client library exposes this feature through the CloudBlobDirectory class.

The CloudBlobDirectory class provides methods allowing blobs to be enumerated in a way that takes account of the directory structure built into the naming convention used. A blob name can include multiple levels of directory.

A CloudBlobDirectory object can be created using either CloudBlobClient. GetDirectoryReference() or CloudBlobContainer.GetDirectoryReference(). The CloudBlobDirectory object refers to a particular level in the directory hierarchy. The blobs and directories in this directory can be retrieved using the synchronous ListBlobs() method, the asynchronous BeginListBlobsSegmented() method, and EndListBlobsSegmented(). The directory list can be pages through using the ListBlobsSegmented() method.

The various methods to list blobs provide the list as an IEnumerable<IListBlobItem>. The IListBlobItem interface is actually the base interface for CloudBlob, CloudBlockBlob, CloudPageBlob, and CloudBlobDirectory. Consequently, any given item in the list could be from any of these classes, so care must be taken that the item is handled correctly. This is similar to the way in which a file needs to be handled differently from a subdirectory when processing the contents of a directory in a file system.

The CloudBlobDirectory class also exposes methods that retrieve a strongly typed CloudBlockBlob, CloudPageBlob, or CloudBlobDirectory with a specified name that may include multiple levels in the directory hierarchy.

In this recipe, we will learn how to generate a list of the contents of a directory and traverse a directory hierarchy.

How to do it...

We are going to list the top-level contents of a directory. Then we will get the top-level directories and list their contents. We do this as follows:

  1. Add a new class named BlobDirectoryExample 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;
  5. Add the following member to the class:
    private CloudBlobContainer cloudBlobContainer;
  6. Add the following constructor to the class:
    public BlobDirectoryExample(String containerName)
    {
      CloudStorageAccount cloudStorageAccount =CloudStorageAccount.Parse(
        ConfigurationManager.AppSettings["DataConnectionString"]);
      CloudBlobClient cloudBlobClient =cloudStorageAccount.CreateCloudBlobClient();
      cloudBlobContainer =cloudBlobClient.GetContainerReference(containerName);
    }
  7. Add a method, creating a container and adding blobs to it, to the class:
    private void CreateContainerAndBlobs(String containerName)
    {
      cloudBlobContainer.CreateIfNotExist();
      CreateBlob("Yosemite/ElCapitan");
      CreateBlob("Yosemite/ElCapitan/TheNose");
      CreateBlob("Yosemite/ElCapitan/SalatheWall");
      CreateBlob("Yosemite/HalfDome");
      CreateBlob("Yosemite/HalfDome/NorthRidge");
      CreateBlob("Yosemite/HalfDome/NorthWestFace");
      CreateBlob("Yosemite/HalfDome/Trail");
    }
  8. Add a method, uploading a blob, to the class:
    private void CreateBlob(String blobName)
    {
      CloudBlockBlob cloudBlockBlob =cloudBlobContainer.GetBlockBlobReference(blobName);
      cloudBlockBlob.UploadText("To be, or not to be");
    }
  9. Add a method, listing the top-level directory items, to the class:
    private void ListTopLevelItems(String directoryName)
    {
      CloudBlobDirectory cloudBlobDirectory =cloudBlobContainer.GetDirectoryReference(directoryName);
    
      IEnumerable<IListBlobItem> blobItems =cloudBlobDirectory.ListBlobs();
      foreach (IListBlobItem blobItem in blobItems)
      {
        Uri uri = blobItem.Uri;
      }
    }
  10. Add a method, traversing the directory tree, to the class:
    private void TraverseDirectoryTree(String directoryName)
    {
      CloudBlobDirectory cloudBlobDirectory =cloudBlobContainer.GetDirectoryReference(directoryName);
    
      IEnumerable<IListBlobItem> blobItems =cloudBlobDirectory.ListBlobs();
      foreach (CloudBlobDirectory cloudBlobDirectoryItem inblobItems.OfType<CloudBlobDirectory>())
      {
        Uri uri = cloudBlobDirectoryItem.Uri;
    
        IEnumerable<CloudBlockBlob> leafBlobs =cloudBlobDirectoryItem.ListBlobs().OfType<CloudBlockBlob>();
        foreach (CloudBlockBlob leafBlockBlob in leafBlobs)
        {
          Uri leafUri = leafBlockBlob.Uri;
        }
      }
    }
  11. Add the following method, using the methods added earlier, to the class:
    public static void UseBlobDirectoryExample()
    {
      String containerName = "{CONTAINER_NAME}";
      String directoryName = "Yosemite";
    
      BlobDirectoryExample blobDirectoryExample =new BlobDirectoryExample(containerName);
       blobDirectoryExample.CreateContainerAndBlobs(containerName);
      blobDirectoryExample.ListTopLevelItems(directoryName);
      blobDirectoryExample.TraverseDirectoryTree(directoryName);
    }
  12. Add the following to the configuration section of app.config:
    <appSettings>
      <add key="DataConnectionString" value="DefaultEndpointsProtocol=http;AccountName={ACCOUNT_NAME};AccountKey={ACCOUNT_KEY}"/> 
    </appSettings>

How it works...

In steps 1 through 4, we set up the BlobDirectoryExample class. In step 5, we add a member which we initialize in the constructor we add in step 6.

In step 7, we create a container and then invoke CreateBlob() multiple times to create the blobs we use in the example. In step 8, we add CreateBlob() to upload a blob.

In step 9, we add a ListTopLevelItems() method that uses a CloudBlobContainer object for the current container to retrieve a CloudBlobDirectory object for the specified directory. We then invoke CloudBlobDirectory.ListBlobs() to generate a directory listing query which we invoke and enumerate. With the example blobs created earlier, this corresponds to the directory listing of Yosemite that retrieves two blobs and two directories.

In step 10, we add a TraverseDirectoryTree() method that uses a CloudBlobContainer object for the current container to retrieve a CloudBlobDirectory object for the specified directory. We then use CloudBlobDirectory.ListBlobs() to generate a directory listing query which we invoke and enumerate over while restricting the enumeration to only CloudBlobDirectory objects. We then use these CloudBlobDirectory objects as the base of separate invocations CloudBlobDirectory.ListBlobs() to retrieve lists of their contents.

In step 11, we add a method to use the methods we added earlier. We must replace {CONTAINER_NAME} with an appropriate container name.

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 account name and access key.

主站蜘蛛池模板: 元江| 涿鹿县| 嘉兴市| 西华县| 宝坻区| 嘉兴市| 纳雍县| 关岭| 西和县| 宁陕县| 阿拉善右旗| 曲阳县| 舞钢市| 林西县| 清水河县| 泾源县| 科尔| 怀远县| 班戈县| 东乡县| 乐平市| 澎湖县| 红桥区| 惠来县| 四子王旗| 蒙阴县| 丹巴县| 栾城县| 芦山县| 定安县| 高尔夫| 榆林市| 房山区| 博野县| 林芝县| 龙泉市| 泗洪县| 清水河县| 泗洪县| 克山县| 桦川县|