- 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.
- Joomla! 1.5 Site Blueprints
- WordPress 2.7 Cookbook
- GIMP 2.6 cookbook
- 中文版Premiere影視編輯課堂實錄
- Solid Works 2021產品設計標準教程
- 剪映:短視頻剪輯/字幕/動畫/AI從新手到高手(手機版+電腦版)
- Maya 2019三維動畫基礎案例教程
- Plone 3 Multimedia
- SolidWorks 2018有限元:運動仿真與流場分析自學手冊
- Capture One 22 Pro高級實戰教程
- 零基礎學Premiere Pro短視頻制作
- .NET 4.0 Generics
- SolidWorks 2017基礎與實例教程
- Photoshop CC 2015課堂實錄
- 全鏈路UI設計:創意思維+項目實戰+就業指導