Skip to content

Latest commit

 

History

History
145 lines (92 loc) · 8.16 KB

storage-blobs-list.md

File metadata and controls

145 lines (92 loc) · 8.16 KB
titletitleSuffixdescriptionservicesauthorms.authorms.servicems.topicms.datems.devlangms.custom
List blobs with .NET
Azure Storage
Learn how to list blobs in your storage account using the Azure Storage client library for .NET. Code examples show how to list blobs in a flat listing, or how to list blobs hierarchically, as though they were organized into directories or folders.
storage
pauljewellmsft
pauljewell
azure-blob-storage
how-to
08/05/2024
csharp
devx-track-csharp, devguide-csharp, devx-track-dotnet

List blobs with .NET

[!INCLUDE storage-dev-guide-selector-list-blob]

This article shows how to list blobs using the Azure Storage client library for .NET.

[!INCLUDE storage-dev-guide-prereqs-dotnet]

Set up your environment

[!INCLUDE storage-dev-guide-project-setup-dotnet]

Authorization

The authorization mechanism must have the necessary permissions to list a blob. For authorization with Microsoft Entra ID (recommended), you need Azure RBAC built-in role Storage Blob Data Reader or higher. To learn more, see the authorization guidance for List Blobs (REST API).

About blob listing options

When you list blobs from your code, you can specify a number of options to manage how results are returned from Azure Storage. You can specify the number of results to return in each set of results, and then retrieve the subsequent sets. You can specify a prefix to return blobs whose names begin with that character or string. And you can list blobs in a flat listing structure, or hierarchically. A hierarchical listing returns blobs as though they were organized into folders.

To list the blobs in a storage account, call one of these methods:

Manage how many results are returned

By default, a listing operation returns up to 5000 results at a time, but you can specify the number of results that you want each listing operation to return. The examples presented in this article show you how to return results in pages. To learn more about pagination concepts, see Pagination with the Azure SDK for .NET.

Filter results with a prefix

To filter the list of blobs, specify a string for the prefix parameter. The prefix string can include one or more characters. Azure Storage then returns only the blobs whose names start with that prefix.

Return metadata

You can return blob metadata with the results by specifying the Metadata value for the BlobTraits enumeration.

Flat listing versus hierarchical listing

Blobs in Azure Storage are organized in a flat paradigm, rather than a hierarchical paradigm (like a classic file system). However, you can organize blobs into virtual directories in order to mimic a folder structure. A virtual directory forms part of the name of the blob and is indicated by the delimiter character.

To organize blobs into virtual directories, use a delimiter character in the blob name. The default delimiter character is a forward slash (/), but you can specify any character as the delimiter.

If you name your blobs using a delimiter, then you can choose to list blobs hierarchically. For a hierarchical listing operation, Azure Storage returns any virtual directories and blobs beneath the parent object. You can call the listing operation recursively to traverse the hierarchy, similar to how you would traverse a classic file system programmatically.

Use a flat listing

By default, a listing operation returns blobs in a flat listing. In a flat listing, blobs are not organized by virtual directory.

The following example lists the blobs in the specified container using a flat listing, with an optional segment size specified, and writes the blob name to a console window.

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/dotnet-v12/CRUD.cs" id="Snippet_ListBlobsFlatListing":::

The sample output is similar to:

Blob name: FolderA/blob1.txtBlob name: FolderA/blob2.txtBlob name: FolderA/blob3.txtBlob name: FolderA/FolderB/blob1.txtBlob name: FolderA/FolderB/blob2.txtBlob name: FolderA/FolderB/blob3.txtBlob name: FolderA/FolderB/FolderC/blob1.txtBlob name: FolderA/FolderB/FolderC/blob2.txtBlob name: FolderA/FolderB/FolderC/blob3.txt

Note

The sample output shown assumes that you have a storage account with a flat namespace. If you've enabled the hierarchical namespace feature for your storage account, directories are not virtual. Instead, they are concrete, independent objects. As a result, directories appear in the list as zero-length blobs.

For an alternative listing option when working with a hierarchical namespace, see List directory contents (Azure Data Lake Storage).

Use a hierarchical listing

When you call a listing operation hierarchically, Azure Storage returns the virtual directories and blobs at the first level of the hierarchy.

To list blobs hierarchically, call the BlobContainerClient.GetBlobsByHierarchy, or the BlobContainerClient.GetBlobsByHierarchyAsync method.

The following example lists the blobs in the specified container using a hierarchical listing, with an optional segment size specified, and writes the blob name to the console window.

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/dotnet-v12/CRUD.cs" id="Snippet_ListBlobsHierarchicalListing":::

The sample output is similar to:

Virtual directory prefix: FolderA/Blob name: FolderA/blob1.txtBlob name: FolderA/blob2.txtBlob name: FolderA/blob3.txtVirtual directory prefix: FolderA/FolderB/Blob name: FolderA/FolderB/blob1.txtBlob name: FolderA/FolderB/blob2.txtBlob name: FolderA/FolderB/blob3.txtVirtual directory prefix: FolderA/FolderB/FolderC/Blob name: FolderA/FolderB/FolderC/blob1.txtBlob name: FolderA/FolderB/FolderC/blob2.txtBlob name: FolderA/FolderB/FolderC/blob3.txt

Note

Blob snapshots cannot be listed in a hierarchical listing operation.

List blob versions or snapshots

To list blob versions or snapshots, specify the BlobStates parameter with the Version or Snapshot field. Versions and snapshots are listed from oldest to newest.

The following code example shows how to list blob versions.

:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/dotnet-v12/CRUD.cs" id="Snippet_ListBlobVersions":::

Resources

To learn more about how to list blobs using the Azure Blob Storage client library for .NET, see the following resources.

REST API operations

The Azure SDK for .NET contains libraries that build on top of the Azure REST API, allowing you to interact with REST API operations through familiar .NET paradigms. The client library methods for listing blobs use the following REST API operation:

[!INCLUDE storage-dev-guide-resources-dotnet]

See also

[!INCLUDE storage-dev-guide-next-steps-dotnet]

close