Azure Blob Storage下载文件问题

5

我正在开发一个功能,允许用户下载Azure Blob Storage中的项目。

我正在尝试使用以下方法获取Blob列表:

 var list = await container.GetBlobsAsync(BlobTraits.All, BlobStates.All, string.Empty).ConfigureAwait(false);

这是我遇到的错误:

Error CS1061 'ConfiguredCancelableAsyncEnumerable' 中不包含“GetAwaiter”的定义,并且找不到可访问的扩展方法“GetAwaiter”,接受类型为“ConfiguredCancelableAsyncEnumerable”的第一个参数(是否缺少使用指令或程序集引用?)

C# 7.3支持异步吗?或者说,如果我想要使用异步调用来获取容器中的所有blob,我需要升级到C# 8.0吗?

如果我将代码更改为以下内容:

            await foreach (BlobItem page in container.GetBlobsAsync(BlobTraits.None, BlobStates.None, string.Empty))
            {
                yield return container.GetBlobClient(page.Name);
            }

然后我遇到了这个错误:

错误 CS8370 特性“异步流”在 C# 7.3 中不可用。 请使用语言版本 8.0 或更高版本。

我知道 GetBlobsAsync() 返回 AsyncPageable<>,并且我假设它仅在 C# 8.0 可用?

1个回答

10

我能想到以下两种选择:

  1. update you're langVersion to 8 which you are saying you do not want to do
  2. use an enumerator eg

    var blobs = blobContainerClient.GetBlobsAsync()
    List<BlobItem> blobList = new List<BlobItem>();
    IAsyncEnumerator<BlobItem> enumerator = blobs.GetAsyncEnumerator();
    try
    {
        while (await enumerator.MoveNextAsync())
        {
            blobList.Add(enumerator.Current);
        }
    }
    finally
    {
        await enumerator.DisposeAsync();
    }
    

我需要在csproj文件中添加什么?或者这就是我升级到C# 8.0的方法?虽然我认为我们还没有准备好使用C# 8.0。 - pg2727
感谢您的帮助和抽出时间查看我的问题! - pg2727
1
这个救了我一命,不用升级Lang版本到8,也不用更新一堆NuGet包。谢谢! - Jeffrey Holmes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接