Skip to content

Latest commit

 

History

History
119 lines (70 loc) · 7.45 KB

storage-blob-download.md

File metadata and controls

119 lines (70 loc) · 7.45 KB
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

Download a blob with .NET

[!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]

Set up your environment

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

Authorization

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

Download a blob

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:

Download to a file path

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":::

Download to a stream

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":::

Download to a string

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":::

Download from a stream

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":::

Download a block blob with configuration options

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.

Specify data transfer options on download

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.

Specify transfer validation options on download

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.

Resources

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

Code samples

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 downloading blobs use the following REST API operation:

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

See also

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