如何从CloudBlobDirectory.ListBlobs获得对BlockBlob对象的引用? [英] How can I get references to BlockBlob objects from CloudBlobDirectory.ListBlobs?
问题描述
我正在使用Microsoft Azure .NET客户端库与Azure云存储进行交互.我需要能够访问有关其元数据集合中每个Blob的其他信息.我目前正在使用CloudBlobDirectory.ListBlobs()方法来获取我在Blob名称中设计的目录结构的特定目录中的Blob列表. ListBlobs()方法返回IListBlobItem对象的列表.它们只有几个属性:网址以及对父目录和父容器的引用.我需要获取实际blob对象的元数据.
I am using the Microsoft Azure .NET client libraries to interact with Azure cloud storage. I need to be able to access additional information about each blob in its metadata collection. I am currently using CloudBlobDirectory.ListBlobs() method to get a list of blobs in a particular directory of a directory structure I've devised in the blob names. The ListBlobs() method returns a list of IListBlobItem objects. They only have a couple of properties: Url and references to parent directory and parent container. I need to get to the metadata of the actual blob objects.
我设想会有一种方法可以将IListBlobItem强制转换为BlockBlob对象,也可以使用IListBlockItem获取对BlockBlob的引用,但似乎找不到找到此方法的方法.
I envisioned there would be a way to either cast the IListBlobItem to a BlockBlob object or use the IListBlockItem to get a reference to the BlockBlob, but can't seem to find a way to do that.
我的问题是:是否可以通过这种方法获取BlockBlob对象,还是必须使用其他方法获取实际的BlockBlob对象?如果有所不同,那么您可以提出一种实现此目标的方法,同时还可以通过目录"方案进行过滤吗?
My question is: Is there a way to get a BlockBlob object from this method, or do I have to use a different way of getting the actual BlockBlob objects? If different, then can you suggest a way to achieve this, while also being able to filter by the "directory" scheme?
推荐答案
好...我找到了一种方法,虽然看起来有些笨拙和间接,但它确实实现了我认为应该可行的主要功能,它将IListBlobItem直接转换为CloudBlockBlob对象.
OK... I found a way to do this, and while it seems a little clunky and indirect, it does achieve the main thing I thought should be doable, which is to cast the IListBlobItem directly to a CloudBlockBlob object.
我正在做的是从Directory对象的ListBlobs()方法获取列表,然后遍历列表中的每个项目,然后将该项目转换为CloudBlockBlob对象,然后调用FetchAttributes()方法以检索属性(包括元数据).然后将新的"info"对象添加到新的info对象列表中.这是我正在使用的代码:
What I am doing is getting the list from the Directory object's ListBlobs() method and then looping over each item in the list and casting the item to a CloudBlockBlob object and then calling the FetchAttributes() method to retrieve the properties (including the metadata). Then add a new "info" object to a new list of info objects. Here's the code I'm using:
CloudBlobDirectory dir = container.GetDirectoryReference(dirPath);
var blobs = dir.ListBlobs(true);
foreach (IListBlobItem item in blobs)
{
CloudBlockBlob blob = (CloudBlockBlob)item;
blob.FetchAttributes();
files.Add(new ImageInfo
{
FileUrl = item.Uri.ToString(),
FileName = item.Uri.PathAndQuery.Replace(restaurantId.ToString().PadLeft(3, '0') + "/", ""),
ImageName = blob.Metadata["Name"]
});
}
整个"Blob"概念似乎不必要地复杂,并且似乎无法实现我原本认为是Blob包装器的主要功能之一.也就是说,通过允许查询名称,目录,容器和元数据来扩展搜索功能的方法.我以为您可以构造一个linq查询,其内容应类似于:返回'图像'容器中所有blob的列表,这些列表位于'natural/landscapes/'目录路径中,且元数据键为'类别",其值为日落"".似乎没有办法做到这一点,这对我来说似乎是一个错失的机会.哦,很好.
The whole "Blob" concept seems needlessly complex and doesn't seem to achieve what I'd have thought would have been one of the main features of the Blob wrapper. That is, a way to expand search capabilities by allowing a query over name, directory, container and metadata. I'd have thought you could construct a linq query that would read somewhat like: "return a list of all blobs in the 'images' container, that are in the 'natural/landscapes/' directory path that have a metadata key of 'category' with the value of 'sunset'". There doesn't seem to be a way to do that and that seems to be a missed opportunity to me. Oh, well.
如果我错了并且离开这里,请告诉我.
If I'm wrong and way off base here, please let me know.
这篇关于如何从CloudBlobDirectory.ListBlobs获得对BlockBlob对象的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!