C# Azure存储-使用v12 SDK for.NET获取容器中的blob列表

C# Azure存储-使用v12 SDK for.NET获取容器中的blob列表,c#,azure-blob-storage,azure-sdk-.net,C#,Azure Blob Storage,Azure Sdk .net,我正在尝试使用列出Azure存储帐户容器中的Blob。然而,我失败了,我怀疑我只是在做一些愚蠢的事情 我在一个类中有一个获取blob的方法,它似乎按照预期工作- public AsyncPageable<BlobItem> GetBlobs(BlobContainerClient container) { return container.GetBlobsAsync(); } 为了完整起见,这里是项类 class Item { public string Name;

我正在尝试使用列出Azure存储帐户容器中的Blob。然而,我失败了,我怀疑我只是在做一些愚蠢的事情

我在一个类中有一个获取blob的方法,它似乎按照预期工作-

public AsyncPageable<BlobItem> GetBlobs(BlobContainerClient container)
{
    return container.GetBlobsAsync();
}
为了完整起见,这里是

class Item
{
    public string Name;
    public string ContentType;

    public Item(BlobItem blob)
    {
        BlobItemProperties properties = blob.Properties;

        this.Name = blob.Name;
        this.ContentType = properties.ContentType;
    }

}

也许您可以尝试将
GetItems
更改为以下形式:

        public async void GetItems(AsyncPageable<BlobItem> blobs)
        {
            this.Items = new List<Item>();
            IAsyncEnumerator<BlobItem> enumerator = blobs.GetAsyncEnumerator();
            try
            {
                while (await enumerator.MoveNextAsync())
                {
                    this.Items.Add(new Item(enumerator.Current));
                }
            }
            finally
            {
                await enumerator.DisposeAsync();
            }
        }
public异步void GetItems(异步可分页blob)
{
this.Items=新列表();
IAsyncEnumerator枚举器=blobs.GetAsyncEnumerator();
尝试
{
while(等待枚举器.MoveNextAsync())
{
this.Items.Add(新项(枚举数.Current));
}
}
最后
{
等待枚举器。DisposeAsync();
}
}

经过进一步的研究,我发现了工厂模式,并使用它,我现在看到
列表项在每次调用后正确填充

class GetBinResponseBody : ResponseBody
{
    public Bin Bin;
    public List<Item> Items = new List<Item>();

    public static GetBinResponseBody CreateAsync(BlobContainerClient container, AsyncPageable<BlobItem> blobs)
    {
        return new GetBinResponseBody().InitializeAsync(container, blobs).Result;
    }

    private async Task<GetBinResponseBody> InitializeAsync(BlobContainerClient container, AsyncPageable<BlobItem> blobs)
    {
        this.Bin = new Bin(container);
        await foreach (BlobItem blob in blobs)
        {
            this.Items.Add(new Item(blob));
        }
        return this;
    }
}
类GetBinResponseBody:ResponseBody
{
公共垃圾箱;
公共列表项=新列表();
公共静态GetBinResponseBy CreateAncy(BlobContainerClient容器,AsyncPageable Blob)
{
返回新的getBinResponseBy().InitializeAsync(容器,blob).Result;
}
专用异步任务InitializeAsync(BlobContainerClient容器,AsyncPageable Blob)
{
this.Bin=新的Bin(容器);
等待foreach(BlobItem blob在blob中)
{
此.Items.Add(新项(blob));
}
归还这个;
}
}

谢谢,但这是我最初看到的,我看到了完全相同的行为。我相信我现在已经用工厂模式让它工作了。
class GetBinResponseBody : ResponseBody
{
    public Bin Bin;
    public List<Item> Items = new List<Item>();

    public static GetBinResponseBody CreateAsync(BlobContainerClient container, AsyncPageable<BlobItem> blobs)
    {
        return new GetBinResponseBody().InitializeAsync(container, blobs).Result;
    }

    private async Task<GetBinResponseBody> InitializeAsync(BlobContainerClient container, AsyncPageable<BlobItem> blobs)
    {
        this.Bin = new Bin(container);
        await foreach (BlobItem blob in blobs)
        {
            this.Items.Add(new Item(blob));
        }
        return this;
    }
}