- 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.
- Beginning Swift
- 基于元胞自動(dòng)機(jī)的城市路網(wǎng)交通流建模與仿真
- 中文版CorelDRAW 2022基礎(chǔ)教程
- Photoshop圖形圖像設(shè)計(jì)案例教程(高等院校計(jì)算機(jī)任務(wù)驅(qū)動(dòng)教改教材)
- 3ds max & VRay產(chǎn)品造型設(shè)計(jì)經(jīng)典
- 圖像處理基礎(chǔ)教程(Photoshop CS5)(第2版)
- 光影之書(shū):Photoshop+Camera Raw攝影后期與創(chuàng)意合成
- ADOBE FLASH PROFESSIONAL CS6 標(biāo)準(zhǔn)培訓(xùn)教材
- UG NX 9中文版從入門到精通
- 神奇的中文版Photoshop CC 2017入門書(shū)
- 無(wú)師自通AutoCAD 2014中文版機(jī)械設(shè)計(jì)
- TopSolid Wood軟件設(shè)計(jì)技術(shù)與應(yīng)用
- 攝影師的后期必修課(調(diào)色篇)
- Origin科技繪圖與數(shù)據(jù)分析
- 魔法詞典:AI繪畫(huà)關(guān)鍵詞圖鑒(Midjourney版)