How can I get references to BlockBlob objects from CloudBlobDirectory.ListBlobs?

后端 未结 3 2143
长发绾君心
长发绾君心 2021-02-07 02:53

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 col

3条回答
  •  孤独总比滥情好
    2021-02-07 03:46

    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.

    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"]
        });
    }
    

    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.

提交回复
热议问题