从 C# 读取 Azure blob 存储中的文件

Read files in Azure blob storage from C#

我在 Azure 存储中有一个 Blob 容器,然后是一个文件夹,然后是一个子文件夹,然后是不同的文件 (ContainerName/Folder1/Subfolder1/files...)。 如何读取 Subfolder1 目录中的所有文件?假设我有一些 pdf 文件,我需要在我的应用程序中获取它们,我该怎么做?

好吧,它并不是真正的子文件夹,它只是一个路径。

正如 documentation for ListBlobs 所说:

You can optionally specify a blob prefix to list blobs whose names begin with the same string. If you use a delimiter character in your blob names to create a virtual directory structure, the blob prefix can include all or part of the virtual directory structure (but not the container name).

所以你会使用 pass in Folder1/Subfolder1 作为前缀:

var container = blobClient.GetContainerReference(containerName);
foreach (var file in container.ListBlobs(prefix: "Folder1/Subfolder1", useFlatBlobListing: true))
{ 
    // etc

注意:我不记得前缀是需要前导斜杠还是尾随斜杠,或者两者都需要,或者两者都不需要..

prefix 参数确保仅以参数值开头的 blob 名称将作为列表的一部分返回。 useFlatBlobListing 参数将确保如果在前缀中指定的子文件夹内的嵌套文件夹中有任何 blob,也会返回。