Skip to content

Commit ee613df

Browse files
Update .NET upload/download articles
1 parent 32856a8 commit ee613df

File tree

2 files changed

+79
-236
lines changed

2 files changed

+79
-236
lines changed

articles/storage/blobs/storage-blob-download.md

Lines changed: 34 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services: storage
66
author: pauljewellmsft
77

88
ms.author: pauljewell
9-
ms.date: 03/28/2022
9+
ms.date: 04/21/2023
1010
ms.service: storage
1111
ms.subservice: blobs
1212
ms.topic: how-to
@@ -16,91 +16,58 @@ ms.custom: devx-track-csharp, devguide-csharp
1616

1717
# Download a blob with .NET
1818

19-
This article shows how to download a blob using the [Azure Storage client library for .NET](/dotnet/api/overview/azure/storage). You can download a blob by using any of the following methods:
19+
This article shows how to download a blob using the [Azure Storage client library for .NET](/dotnet/api/overview/azure/storage). 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.
20+
21+
## Prerequisites
22+
23+
To work with the code examples in this article, make sure you have:
24+
25+
- An authorized client object to connect to Blob Storage data resources. To learn more, see [Create and manage client objects that interact with data resources](storage-blob-client-management.md).
26+
- Permissions to perform an upload operation. To learn more, see the authorization guidance for the following REST API operations:
27+
- [Get Blob](/rest/api/storageservices/get-blob#authorization)
28+
- Packages installed to your project directory. These examples use **Azure.Storage.Blobs**. If you're using `DefaultAzureCredential` for authorization, you also need **Azure.Identity**. To learn more about setting up your project, see [Get Started with Azure Storage and .NET](storage-blob-dotnet-get-started.md#set-up-your-project).
29+
30+
## About downloading blobs
31+
32+
The [Get Blob](/rest/api/storageservices/put-blob) operation reads or downloads a blob from Azure Storage, including its metadata and properties. To learn more about the `Get Blob` operation, including timeout parameters and error conditions, see [Get Blob remarks](/rest/api/storageservices/get-blob#remarks).
33+
34+
## Download a blob
35+
36+
You can download a blob by using any of the following methods:
2037

2138
- [DownloadTo](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadto)
2239
- [DownloadToAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadtoasync)
2340
- [DownloadContent](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadcontent)
2441
- [DownloadContentAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadcontentasync)
2542

26-
You can also open a stream to read from a blob. The stream will only download the blob as the stream is read from. Use either of the following methods:
43+
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:
2744

2845
- [OpenRead](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.openread)
2946
- [OpenReadAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.openreadasync)
30-
31-
> [!NOTE]
32-
> The examples in this article assume that you've created a [BlobServiceClient](/dotnet/api/azure.storage.blobs.blobserviceclient) object by using the guidance in the [Get started with Azure Blob Storage and .NET](storage-blob-dotnet-get-started.md) article.
3347

3448
## Download to a file path
3549

36-
The following example downloads a blob by using a file path. If the specified directory does not exist, handle the exception and notify the user.
37-
38-
```csharp
39-
public static async Task DownloadBlob(BlobClient blobClient, string localFilePath)
40-
{
41-
try
42-
{
43-
await blobClient.DownloadToAsync(localFilePath);
44-
}
45-
catch (DirectoryNotFoundException ex)
46-
{
47-
// Let the user know that the directory does not exist
48-
Console.WriteLine($"Directory not found: {ex.Message}");
49-
}
50-
}
51-
```
52-
53-
If the file already exists at `localFilePath`, it will be overwritten by default during subsequent downloads.
50+
The following example downloads a blob to a local file path. If the specified directory doesn't exist, the code throws a [DirectoryNotFoundException](/dotnet/api/system.io.directorynotfoundexception). If the file already exists at `localFilePath`, it's overwritten by default during subsequent downloads.
51+
52+
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobToFile":::
5453

5554
## Download to a stream
5655

57-
The following example downloads a blob by creating a [Stream](/dotnet/api/system.io.stream) object and then downloads to that stream. If the specified directory does not exist, handle the exception and notify the user.
58-
59-
```csharp
60-
public static async Task DownloadToStream(BlobClient blobClient, string localFilePath)
61-
{
62-
try
63-
{
64-
FileStream fileStream = File.OpenWrite(localFilePath);
65-
await blobClient.DownloadToAsync(fileStream);
66-
fileStream.Close();
67-
}
68-
catch (DirectoryNotFoundException ex)
69-
{
70-
// Let the user know that the directory does not exist
71-
Console.WriteLine($"Directory not found: {ex.Message}");
72-
}
73-
}
74-
```
56+
The following example downloads a blob by creating a [Stream](/dotnet/api/system.io.stream) object and then downloads to that stream. If the specified directory doesn't exist, the code throws a [DirectoryNotFoundException](/dotnet/api/system.io.directorynotfoundexception).
57+
58+
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobToStream":::
7559

7660
## Download to a string
7761

78-
The following example downloads a blob to a string. This example assumes that the blob is a text file.
62+
The following example assumes that the blob is a text file, and downloads the blob to a string:
7963

80-
```csharp
81-
public static async Task DownloadToText(BlobClient blobClient)
82-
{
83-
BlobDownloadResult downloadResult = await blobClient.DownloadContentAsync();
84-
string downloadedData = downloadResult.Content.ToString();
85-
Console.WriteLine("Downloaded data:", downloadedData);
86-
}
87-
```
64+
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobToString":::
8865

8966
## Download from a stream
9067

91-
The following example downloads a blob by reading from a stream.
68+
The following example downloads a blob by reading from a stream:
9269

93-
```csharp
94-
public static async Task DownloadfromStream(BlobClient blobClient, string localFilePath)
95-
{
96-
using (var stream = await blobClient.OpenReadAsync())
97-
{
98-
FileStream fileStream = File.OpenWrite(localFilePath);
99-
await stream.CopyToAsync(fileStream);
100-
}
101-
}
102-
103-
```
70+
:::code language="csharp" source="~/azure-storage-snippets/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs" id="Snippet_DownloadBlobFromStream":::
10471

10572
## Resources
10673

@@ -112,4 +79,8 @@ The Azure SDK for .NET contains libraries that build on top of the Azure REST AP
11279

11380
- [Get Blob](/rest/api/storageservices/get-blob) (REST API)
11481

82+
### Code samples
83+
84+
- [View code samples from this article (GitHub)](https://github.com/Azure-Samples/AzureStorageSnippets/blob/master/blobs/howto/dotnet/BlobDevGuideBlobs/DownloadBlob.cs)
85+
11586
[!INCLUDE [storage-dev-guide-resources-dotnet](../../../includes/storage-dev-guides/storage-dev-guide-resources-dotnet.md)]

0 commit comments

Comments
 (0)