如何区分Blob与文件夹?
我正在使用GetBlobsAsync() 来获取一个文件夹中所有Blob的列表。
List<BlobItem> blobs = container.GetBlobs(prefix: "myfolder").ToList();
这在实际运行中可行,但第一项总是该文件夹的名称。
如何避免返回该文件夹的名称?
或者如何通过属性判断某一项是Blob对象还是文件夹?
解决方案
使用扁平列表,与您当前的情况相反,显示了针对 GetBlobsAsync() 的不包含文件夹的示例输出:
示例输出类似于:
console Blob name: FolderA/blob1.txt Blob name: FolderA/blob2.txt Blob name: FolderA/blob3.txt Blob name: FolderA/FolderB/blob1.txt Blob name: FolderA/FolderB/blob2.txt Blob name: FolderA/FolderB/blob3.txt Blob name: FolderA/FolderB/FolderC/blob1.txt Blob name: FolderA/FolderB/FolderC/blob2.txt Blob name: FolderA/FolderB/FolderC/blob3.txt
不过,紧接着有一个说明与您情况类似的注释,强调之处为我标注:
注
所示的示例输出假设您拥有一个扁平命名空间的存储账户。如果您已为存储账户启用分层命名空间功能,目录不是虚拟的。相反,它们是具体、独立的对象。因此,目录在列表中显示为零长度的Blob对象。如需在使用分层命名空间时的替代列出选项,请参见List directory contents (Azure Data Lake Storage)。
一个朴素的解决方案可能是检查长度。我假设这指的是 BlobItem.Properties。ContentLength。但你可能也有零长度的非目录Blob,这会使这个条件变得模糊。
鉴于你正以层级关系来思考,并且显然已经以这种方式对你的Blob进行了组织,最好明确表达这一意图,并使用 [使用分层列出](https://learn.microsoft.com/en-us/azure/storage/blobs/storage-blobs-list#use-a-hierarchical-listing),它为你提供了 BlobHierarchyItem.IsPrefix 这一你所需要的属性:
使用分层列出
当你以分层方式调用列出操作时,Azure存储会在层级的第一层返回虚拟目录和Blob。
要按层次结构列出Blob,请调用BlobContainerClient.GetBlobsByHierarchy](/en-us/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobsbyhierarchy),或BlobContainerClient.GetBlobsByHierarchyAsync](/en-us/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobsbyhierarchyasync) 方法。
以下示例使用分层列出在指定容器中列出Blob,并可指定一个可选的段大小,然后将Blob名称写入控制台窗口。
```cs private static async Task ListBlobsHierarchicalListing(BlobContainerClient container, string prefix, int? segmentSize) { try { // Call the listing operation and return pages of the specified size. var resultSegment = container.GetBlobsByHierarchyAsync(prefix:prefix, delimiter:"/") .AsPages(default, segmentSize);
// Enumerate the blobs returned for each page. await foreach (Page<BlobHierarchyItem> blobPage in resultSegment) { // A hierarchical listing may return both virtual directories and blobs. foreach (BlobHierarchyItem blobhierarchyItem in blobPage.Values) { if (blobhierarchyItem.IsPrefix) { // Write out the prefix of the virtual directory. Console.WriteLine("Virtual directory prefix: {0}", blobhierarchyItem.Prefix); // Call recursively with the prefix to traverse the virtual directory. await ListBlobsHierarchicalListing(container, blobhierarchyItem.Prefix, null); } else { // Write out the name of the blob. Console.WriteLine("Blob name: {0}", blobhierarchyItem.Blob.Name); } } Console.WriteLine(); } } catch (RequestFailedException e) { Console.WriteLine(e.Message); Console.ReadLine(); throw; }} ```
示例输出类似于:
```console Virtual directory prefix: FolderA/ Blob name: FolderA/blob1.txt Blob name: FolderA/blob2.txt Blob name: FolderA/blob3.txt
Virtual directory prefix: FolderA/FolderB/ Blob name: FolderA/FolderB/blob1.txt Blob name: FolderA/FolderB/blob2.txt Blob name: FolderA/FolderB/blob3.txt
Virtual directory prefix: FolderA/FolderB/FolderC/ Blob name: FolderA/FolderB/FolderC/blob1.txt Blob name: FolderA/FolderB/FolderC/blob2.txt Blob name: FolderA/FolderB/FolderC/blob3.txt ```
注:
Blob快照不能在分层列出操作中列出。