【发布时间】:2021-02-03 17:59:32
【问题描述】:
我找到了一些使用 WindowsAzure.Storage.Blob 的答案,但是这个包已经过时了。
所以我需要使用 .net 核心从 Azure 获取 Blob 存储中的容器列表。
【问题讨论】:
标签: azure .net-core azure-blob-storage
我找到了一些使用 WindowsAzure.Storage.Blob 的答案,但是这个包已经过时了。
所以我需要使用 .net 核心从 Azure 获取 Blob 存储中的容器列表。
【问题讨论】:
标签: azure .net-core azure-blob-storage
添加“Azure.Storage.Blobs”NuGet 包。
使用 BlobServiceClient 类
string connectionString = "";
BlobServiceClient client = new BlobServiceClient(connectionString);
使用 GetBlobContainers 或 GetBlobContainersAsync 检索容器:
//-------------------------------------------------
// List containers
//-------------------------------------------------
async static Task ListContainers(BlobServiceClient blobServiceClient,
string prefix,
int? segmentSize)
{
string continuationToken = string.Empty;
try
{
do
{
// Call the listing operation and enumerate the result segment.
// When the continuation token is empty, the last segment has been returned
// and execution can exit the loop.
var resultSegment =
blobServiceClient.GetBlobContainersAsync(BlobContainerTraits.Metadata, prefix, default)
.AsPages(continuationToken, segmentSize);
await foreach (Azure.Page<BlobContainerItem> containerPage in resultSegment)
{
foreach (BlobContainerItem containerItem in containerPage.Values)
{
Console.WriteLine("Container name: {0}", containerItem.Name);
}
// Get the continuation token and loop until it is empty.
continuationToken = containerPage.ContinuationToken;
Console.WriteLine();
}
} while (continuationToken != string.Empty);
}
catch (RequestFailedException e)
{
Console.WriteLine(e.Message);
Console.ReadLine();
throw;
}
}
prefix 参数是可选的,以防您要搜索以某个前缀开头的容器。
代码来自documentation:
【讨论】: