Skip to content

Commit d6ebb04

Browse files
Merge pull request #264676 from pauljewellmsft/blob-qs-dotnet
Blob .NET quickstart cleanup
2 parents 7e86127 + 8d50a1f commit d6ebb04

File tree

2 files changed

+51
-53
lines changed

2 files changed

+51
-53
lines changed

articles/storage/blobs/storage-quickstart-blobs-dotnet.md

Lines changed: 41 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
---
22
title: "Quickstart: Azure Blob Storage library - .NET"
3-
description: In this quickstart, you will learn how to use the Azure Blob Storage client library for .NET to create a container and a blob in Blob (object) storage. Next, you learn how to download the blob to your local computer, and how to list all of the blobs in a container.
3+
description: In this quickstart, you learn how to use the Azure Blob Storage client library for .NET to create a container and a blob in Blob (object) storage. Next, you learn how to download the blob to your local computer, and how to list all of the blobs in a container.
44
author: pauljewellmsft
55
ms.author: pauljewell
6-
ms.date: 11/09/2022
6+
ms.date: 01/30/2024
77
ms.service: azure-blob-storage
88
ms.topic: quickstart
99
ms.devlang: csharp
@@ -13,7 +13,9 @@ ai-usage: ai-assisted
1313

1414
# Quickstart: Azure Blob Storage client library for .NET
1515

16-
Get started with the Azure Blob Storage client library for .NET. Azure Blob Storage is Microsoft's object storage solution for the cloud. Follow these steps to install the package and try out example code for basic tasks. Blob storage is optimized for storing massive amounts of unstructured data.
16+
Get started with the Azure Blob Storage client library for .NET. Azure Blob Storage is Microsoft's object storage solution for the cloud, and is optimized for storing massive amounts of unstructured data.
17+
18+
In this article, you follow steps to install the package and try out example code for basic tasks.
1719

1820
[API reference documentation](/dotnet/api/azure.storage.blobs) | [Library source code](https://github.com/Azure/azure-sdk-for-net/tree/master/sdk/storage/Azure.Storage.Blobs) | [Package (NuGet)](https://www.nuget.org/packages/Azure.Storage.Blobs) | [Samples](../common/storage-samples-dotnet.md?toc=/azure/storage/blobs/toc.json#blob-samples)
1921

@@ -34,7 +36,7 @@ This section walks you through preparing a project to work with the Azure Blob S
3436

3537
### Create the project
3638

37-
For the steps ahead, you'll need to create a .NET console app using either the .NET CLI or Visual Studio 2022.
39+
Create a .NET console app using either the .NET CLI or Visual Studio 2022.
3840

3941
### [Visual Studio 2022](#tab/visual-studio)
4042

@@ -46,7 +48,7 @@ For the steps ahead, you'll need to create a .NET console app using either the .
4648

4749
1. For the **Project Name**, enter *BlobQuickstart*. Leave the default values for the rest of the fields and select **Next**.
4850

49-
1. For the **Framework**, ensure .NET 6.0 is selected. Then choose **Create**. The new project will open inside the Visual Studio environment.
51+
1. For the **Framework**, ensure the latest installed version of .NET is selected. Then choose **Create**. The new project opens inside the Visual Studio environment.
5052

5153
### [.NET CLI](#tab/net-cli)
5254

@@ -93,13 +95,13 @@ dotnet add package Azure.Storage.Blobs
9395

9496
If this command to add the package fails, follow these steps:
9597

96-
- Make sure that `nuget.org` is added as a package source. You can list the package sources using the [dotnet nuget list source](/dotnet/core/tools/dotnet-nuget-list-source#examples) command:
98+
- Make sure that `nuget.org` is added as a package source. You can list the package sources using the [`dotnet nuget list source`](/dotnet/core/tools/dotnet-nuget-list-source#examples) command:
9799

98100
```dotnetcli
99101
dotnet nuget list source
100102
```
101103

102-
- If you don't see `nuget.org` in the list, you can add it using the [dotnet nuget add source](/dotnet/core/tools/dotnet-nuget-add-source#examples) command:
104+
- If you don't see `nuget.org` in the list, you can add it using the [`dotnet nuget add source`](/dotnet/core/tools/dotnet-nuget-add-source#examples) command:
103105
104106
```dotnetcli
105107
dotnet nuget add source https://api.nuget.org/v3/index.json -n nuget.org
@@ -123,8 +125,6 @@ using System.IO;
123125
Console.WriteLine("Hello, World!");
124126
```
125127
126-
[!INCLUDE [storage-quickstart-credential-free-include](../../../includes/storage-quickstart-credential-free-include.md)]
127-
128128
## Object model
129129
130130
Azure Blob Storage is optimized for storing massive amounts of unstructured data. Unstructured data doesn't adhere to a particular data model or definition, such as text or binary data. Blob storage offers three types of resources:
@@ -145,24 +145,25 @@ Use the following .NET classes to interact with these resources:
145145

146146
## Code examples
147147

148-
The sample code snippets in the following sections demonstrate how to perform basic data operations with the Azure Blob Storage client library for .NET.
148+
The sample code snippets in the following sections demonstrate how to perform the following tasks with the Azure Blob Storage client library for .NET:
149+
150+
- [Authenticate to Azure and authorize access to blob data](#authenticate-to-azure-and-authorize-access-to-blob-data)
151+
- [Create a container](#create-a-container)
152+
- [Upload a blob to a container](#upload-a-blob-to-a-container)
153+
- [List blobs in a container](#list-blobs-in-a-container)
154+
- [Download a blob](#download-a-blob)
155+
- [Delete a container](#delete-a-container)
149156

150157
> [!IMPORTANT]
151-
> Make sure you have installed the correct NuGet packages and added the necessary using statements in order for the code samples to work, as described in the [setting up](#setting-up) section.
158+
> Make sure you've installed the correct NuGet packages and added the necessary using statements in order for the code samples to work, as described in the [setting up](#setting-up) section.
152159
153-
* **Azure.Identity** (if you are using the passwordless approach)
154-
* **Azure.Storage.Blobs**
160+
[!INCLUDE [storage-quickstart-credential-free-include](../../../includes/storage-quickstart-credential-free-include.md)]
155161
156162
### Create a container
157163
158-
Decide on a name for the new container. The code below appends a GUID value to the container name to ensure that it is unique.
164+
Create a new container in your storage account by calling the [CreateBlobContainerAsync](/dotnet/api/azure.storage.blobs.blobserviceclient.createblobcontainerasync) method on the `blobServiceClient` object. In this example, the code appends a GUID value to the container name to ensure that it's unique.
159165

160-
> [!IMPORTANT]
161-
> Container names must be lowercase. For more information about naming containers and blobs, see [Naming and Referencing Containers, Blobs, and Metadata](/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata).
162-
163-
You can call the [CreateBlobContainerAsync](/dotnet/api/azure.storage.blobs.blobserviceclient.createblobcontainerasync) method on the `blobServiceClient` to create a container in your storage account.
164-
165-
Add this code to the end of the `Program.cs` class:
166+
Add this code to the end of the `Program.cs` file:
166167

167168
```csharp
168169
// TODO: Replace <storage-account-name> with your actual storage account name
@@ -179,8 +180,13 @@ BlobContainerClient containerClient = await blobServiceClient.CreateBlobContaine
179180
180181
To learn more about creating a container, and to explore more code samples, see [Create a blob container with .NET](storage-blob-container-create.md).
181182
183+
> [!IMPORTANT]
184+
> Container names must be lowercase. For more information about naming containers and blobs, see [Naming and Referencing Containers, Blobs, and Metadata](/rest/api/storageservices/naming-and-referencing-containers--blobs--and-metadata).
185+
182186
### Upload a blob to a container
183187
188+
Upload a blob to a container using [UploadAsync](/dotnet/api/azure.storage.blobs.blobclient.uploadasync). The example code creates a text file in the local *data* directory to upload to the container.
189+
184190
Add the following code to the end of the `Program.cs` class:
185191
186192
```csharp
@@ -198,23 +204,17 @@ BlobClient blobClient = containerClient.GetBlobClient(fileName);
198204
199205
Console.WriteLine("Uploading to Blob storage as blob:\n\t {0}\n", blobClient.Uri);
200206
201-
// Upload data from the local file
207+
// Upload data from the local file, overwrite the blob if it already exists
202208
await blobClient.UploadAsync(localFilePath, true);
203209
```
204210
205-
The code snippet completes the following steps:
206-
207-
1. Creates a text file in the local *data* directory.
208-
1. Gets a reference to a [BlobClient](/dotnet/api/azure.storage.blobs.blobclient) object by calling the [GetBlobClient](/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobclient) method on the container from the [Create a container](#create-a-container) section.
209-
1. Uploads the local text file to the blob by calling the [UploadAsync](/dotnet/api/azure.storage.blobs.blobclient.uploadasync#Azure_Storage_Blobs_BlobClient_UploadAsync_System_String_System_Boolean_System_Threading_CancellationToken_) method. This method creates the blob if it doesn't already exist, and overwrites it if it does.
210-
211211
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with .NET](storage-blob-upload.md).
212212
213213
### List blobs in a container
214214
215-
List the blobs in the container by calling the [GetBlobsAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobsasync) method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob.
215+
List the blobs in the container by calling the [GetBlobsAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobsasync) method.
216216
217-
Add the following code to the end of the `Program.cs` class:
217+
Add the following code to the end of the `Program.cs` file:
218218
219219
```csharp
220220
Console.WriteLine("Listing blobs...");
@@ -230,9 +230,9 @@ To learn more about listing blobs, and to explore more code samples, see [List b
230230
231231
### Download a blob
232232
233-
Download the previously created blob by calling the [DownloadToAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadtoasync) method. The example code adds a suffix of "DOWNLOADED" to the file name so that you can see both files in local file system.
233+
Download the blob we created earlier by calling the [DownloadToAsync](/dotnet/api/azure.storage.blobs.specialized.blobbaseclient.downloadtoasync) method. The example code appends the string "DOWNLOADED" to the file name so that you can see both files in local file system.
234234
235-
Add the following code to the end of the `Program.cs` class:
235+
Add the following code to the end of the `Program.cs` file:
236236
237237
```csharp
238238
// Download the blob to a local file
@@ -250,11 +250,11 @@ To learn more about downloading blobs, and to explore more code samples, see [Do
250250
251251
### Delete a container
252252
253-
The following code cleans up the resources the app created by deleting the entire container by using [DeleteAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.deleteasync). It also deletes the local files created by the app.
253+
The following code cleans up the resources the app created by deleting the container using [DeleteAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.deleteasync). The code example also deletes the local files created by the app.
254254
255-
The app pauses for user input by calling `Console.ReadLine` before it deletes the blob, container, and local files. This is a good chance to verify that the resources were actually created correctly, before they are deleted.
255+
The app pauses for user input by calling `Console.ReadLine` before it deletes the blob, container, and local files. This is a good chance to verify that the resources were created correctly, before they're deleted.
256256
257-
Add the following code to the end of the `Program.cs` class:
257+
Add the following code to the end of the `Program.cs` file:
258258
259259
```csharp
260260
// Clean up
@@ -275,7 +275,7 @@ To learn more about deleting a container, and to explore more code samples, see
275275
276276
## The completed code
277277
278-
After completing these steps the code in your `Program.cs` file should now resemble the following:
278+
After completing these steps, the code in your `Program.cs` file should now resemble the following:
279279
280280
## [Passwordless (Recommended)](#tab/managed-identity)
281281
@@ -424,29 +424,29 @@ dotnet build
424424
dotnet run
425425
```
426426
427-
The output of the app is similar to the following example:
427+
The output of the app is similar to the following example (GUID values omitted for readability):
428428
429429
```output
430430
Azure Blob Storage - .NET quickstart sample
431431
432432
Uploading to Blob storage as blob:
433-
https://mystorageacct.blob.core.windows.net/quickstartblobs60c70d78-8d93-43ae-954d-8322058cfd64/quickstart2fe6c5b4-7918-46cb-96f4-8c4c5cb2fd31.txt
433+
https://mystorageacct.blob.core.windows.net/quickstartblobsGUID/quickstartGUID.txt
434434
435435
Listing blobs...
436-
quickstart2fe6c5b4-7918-46cb-96f4-8c4c5cb2fd31.txt
436+
quickstartGUID.txt
437437
438438
Downloading blob to
439-
./data/quickstart2fe6c5b4-7918-46cb-96f4-8c4c5cb2fd31DOWNLOADED.txt
439+
./data/quickstartGUIDDOWNLOADED.txt
440440
441441
Press any key to begin clean up
442442
Deleting blob container...
443443
Deleting the local source and downloaded files...
444444
Done
445445
```
446446
447-
Before you begin the clean up process, check your *data* folder for the two files. You can open them and observe that they are identical.
447+
Before you begin the clean-up process, check your *data* folder for the two files. You can open them and observe that they're identical.
448448
449-
After you've verified the files, press the **Enter** key to delete the test files and finish the demo.
449+
After you verify the files, press the **Enter** key to delete the test files and finish the demo.
450450
451451
## Next steps
452452

includes/storage-quickstart-credential-free-include.md

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,19 @@
22
title: "include file"
33
description: "include file"
44
services: storage
5-
author: alexwolfmsft
5+
author: pauljewellmsft
66
ms.service: azure-storage
77
ms.topic: include
8-
ms.date: 09/09/2022
9-
ms.author: alexwolf
8+
ms.date: 01/30/2024
9+
ms.author: pauljewell
1010
ms.custom: include file
1111
---
1212

13-
## Authenticate to Azure and authorize access to blob data
13+
### Authenticate to Azure and authorize access to blob data
1414

1515
[!INCLUDE [storage-quickstart-passwordless-auth-intro](storage-quickstart-passwordless-auth-intro.md)]
1616

17-
## [Passwordless (Recommended)](#tab/managed-identity)
17+
### [Passwordless (Recommended)](#tab/managed-identity)
1818

1919
`DefaultAzureCredential` is a class provided by the Azure Identity client library for .NET, which you can learn more about on the [DefaultAzureCredential overview](/dotnet/azure/sdk/authentication#defaultazurecredential). `DefaultAzureCredential` supports multiple authentication methods and determines which method should be used at runtime. This approach enables your app to use different authentication methods in different environments (local vs. production) without implementing environment-specific code.
2020

@@ -24,11 +24,11 @@ For example, your app can authenticate using your Visual Studio sign-in credenti
2424

2525
<a name='assign-roles-to-your-azure-ad-user-account'></a>
2626

27-
### Assign roles to your Microsoft Entra user account
27+
#### Assign roles to your Microsoft Entra user account
2828

2929
[!INCLUDE [assign-roles](assign-roles.md)]
3030

31-
### Sign-in and connect your app code to Azure using DefaultAzureCredential
31+
#### Sign in and connect your app code to Azure using DefaultAzureCredential
3232

3333
You can authorize access to data in your storage account using the following steps:
3434

@@ -58,7 +58,7 @@ You can authorize access to data in your storage account using the following ste
5858
> [!NOTE]
5959
> When deployed to Azure, this same code can be used to authorize requests to Azure Storage from an application running in Azure. However, you'll need to enable managed identity on your app in Azure. Then configure your storage account to allow that managed identity to connect. For detailed instructions on configuring this connection between Azure services, see the [Auth from Azure-hosted apps](/dotnet/azure/sdk/authentication-azure-hosted-apps) tutorial.
6060

61-
## [Connection String](#tab/connection-string)
61+
### [Connection String](#tab/connection-string)
6262

6363
A connection string includes the storage account access key and uses it to authorize requests. Always be careful to never expose the keys in an unsecure location.
6464

@@ -67,7 +67,7 @@ A connection string includes the storage account access key and uses it to autho
6767

6868
[!INCLUDE [retrieve credentials](retrieve-credentials.md)]
6969

70-
### Configure your storage connection string
70+
#### Configure your storage connection string
7171

7272
After you copy the connection string, write it to a new environment variable on the local machine running the application. To set the environment variable, open a console window, and follow the instructions for your operating system. Replace `<yourconnectionstring>` with your actual connection string.
7373

@@ -85,9 +85,7 @@ After you add the environment variable in Windows, you must start a new instance
8585
export AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"
8686
```
8787

88-
### Configure the connection string
89-
90-
The code below retrieves the connection string for the storage account from the environment variable created in the [Configure your storage connection string](#configure-your-storage-connection-string) section, and uses the connection string to construct a service client object.
88+
The code below retrieves the connection string for the storage account from the environment variable created earlier, and uses the connection string to construct a service client object.
9189

9290
Add following code to the end of the `Program.cs` file:
9391

0 commit comments

Comments
 (0)