- Microsoft Windows Azure Development Cookbook
- Neil Mackenzie
- 861字
- 2021-04-02 18:51:58
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:
- Add a new class named
BlobDirectoryExample
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;
- Add the following member to the class:
private CloudBlobContainer cloudBlobContainer;
- 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); }
- 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"); }
- 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"); }
- 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; } }
- 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; } } }
- 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); }
- Add the following to the
configuration
section ofapp.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.
- 爸媽微信e時代
- ERP沙盤模擬簡明教程
- Spring Web Flow 2 Web Development
- InDesign平面設計案例教程:從設計到印刷
- Django 1.2 E/commerce
- Mastering phpMyAdmin 3.3.x for Effective MySQL Management
- iPhone Applications Tune/Up
- Photoshop CC 服裝設計經典實例教程
- 零基礎學AutoCAD 2018(全視頻教學版)
- Photoshop CS6標準教程(全視頻微課版)
- Linux Shell Scripting Cookbook
- Quality Assurance for Dynamics AX-Based ERP Solutions
- 中文版3ds Max 2020基礎教程
- Photoshop 2020實戰從入門到精通(超值版)
- Instant GSON