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

后端 未结 3 2146
长发绾君心
长发绾君心 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:45

    The previous answers are good. I just wanted to point out 2 things:

    1) Nowadays ASYNC programming is recommended to do and supported by Azure SDK as well. So try to use it:

    CloudBlobDirectory dir = container.GetDirectoryReference(dirPath);
    var blobs = dir.ListBlobs(true);
    
    foreach (IListBlobItem item in blobs)
    {
        CloudBlockBlob blob = (CloudBlockBlob)item;
        await blob.FetchAttributesAsync(); //Use async calls...
    }
    

    2) Fetching Metadata in a separate call is not efficient. The code makes 2 HTTP request per blob object. ListBlobs() method supports getting Metadata with as well in one call by setting BlobListingDetails parameter:

    CloudBlobDirectory dir = container.GetDirectoryReference(dirPath);
    var blobs = dir.ListBlobs(useFlatBlobListing: true, blobListingDetails: BlobListingDetails.Metadata);
    

    I recommend to use second code it it is possible. Since it is the most efficient way to fetch Metadata.

提交回复
热议问题