title | titleSuffix | description | services | author | ms.author | ms.date | ms.service | ms.topic | ms.devlang | ms.custom |
---|---|---|---|---|---|---|---|---|---|---|
Download a blob with .NET |
Azure Storage |
Learn how to download a blob in Azure Storage by using the .NET client library. |
storage |
pauljewellmsft |
pauljewell |
08/05/2024 |
azure-blob-storage |
how-to |
csharp |
devx-track-csharp, devguide-csharp, devx-track-dotnet |
[!INCLUDE storage-dev-guide-selector-download]
This article shows how to download a blob using the Azure Storage client library for .NET. You can download blob data to various destinations, including a local file path, stream, or text string. You can also open a blob stream and read from it.
[!INCLUDE storage-dev-guide-prereqs-dotnet]
[!INCLUDE storage-dev-guide-project-setup-dotnet]
The authorization mechanism must have the necessary permissions to perform a download operation. 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 Get Blob (REST API).
You can use any of the following methods to download a blob:
You can also open a stream to read from a blob. The stream only downloads the blob as the stream is read from. You can use either of the following methods:
The following example downloads a blob to a local file path. If the specified directory doesn't exist, the code throws a DirectoryNotFoundException. If the file already exists at localFilePath
, it's overwritten by default during subsequent downloads.
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobToFile":::
The following example downloads a blob by creating a Stream object and then downloads to that stream. If the specified directory doesn't exist, the code throws a DirectoryNotFoundException.
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobToStream":::
The following example assumes that the blob is a text file, and downloads the blob to a string:
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobToString":::
The following example downloads a blob by reading from a stream:
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobFromStream":::
You can define client library configuration options when downloading a blob. These options can be tuned to improve performance and enhance reliability. The following code examples show how to use BlobDownloadToOptions to define configuration options when calling a download method. Note that the same options are available for BlobDownloadOptions.
You can configure the values in StorageTransferOptions to improve performance for data transfer operations. The following code example shows how to set values for StorageTransferOptions
and include the options as part of a BlobDownloadToOptions
instance. The values provided in this sample aren't intended to be a recommendation. To properly tune these values, you need to consider the specific needs of your app.
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobWithTransferOptions":::
To learn more about tuning data transfer options, see Performance tuning for uploads and downloads.
You can specify transfer validation options to help ensure that data is downloaded properly and hasn't been tampered with during transit. Transfer validation options can be defined at the client level using BlobClientOptions, which applies validation options to all methods called from a BlobClient instance.
You can also override transfer validation options at the method level using BlobDownloadToOptions. The following code example shows how to create a BlobDownloadToOptions
object and specify an algorithm for generating a checksum. The checksum is then used by the service to verify data integrity of the downloaded content.
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobWithChecksum":::
The following table shows the available options for the checksum algorithm, as defined by StorageChecksumAlgorithm:
Name | Value | Description |
---|---|---|
Auto | 0 | Recommended. Allows the library to choose an algorithm. Different library versions may choose different algorithms. |
None | 1 | No selected algorithm. Don't calculate or request checksums. |
MD5 | 2 | Standard MD5 hash algorithm. |
StorageCrc64 | 3 | Azure Storage custom 64-bit CRC. |
To learn more about how to download blobs using the Azure Blob Storage client library for .NET, see the following resources.
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 downloading blobs use the following REST API operation:
- Get Blob (REST API)
[!INCLUDE storage-dev-guide-resources-dotnet]
[!INCLUDE storage-dev-guide-next-steps-dotnet]