简体   繁体   中英

Read files in Azure blob storage from C#

I have in Azure Storage a Blob Container, then a folder, then a subfolder, and then different files( ContainerName/Folder1/Subfolder1/files... ). How can I read all the files in that Subfolder1 directory? Let's say I have some pdf files in there and I need to get them in my application, how would I do that?

Well, it's not really a subfolder, it's just a path.

As the documentation for ListBlobs says:

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).

So you would use pass in Folder1/Subfolder1 as the prefix:

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

Note: I do not remember offhand whether the prefix needs a leading or trailing slash or both or neither..

prefix parameter ensures that only blobs names of which start with the parameter's value will be returned as part of listing. useFlatBlobListing parameter will ensure that if there are any blobs in the nested folders inside the subfolder specified in prefix are also returned.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM