You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
4
4
author: pauljewellmsft
5
5
ms.author: pauljewell
6
-
ms.date: 11/09/2022
6
+
ms.date: 01/30/2024
7
7
ms.service: azure-blob-storage
8
8
ms.topic: quickstart
9
9
ms.devlang: csharp
@@ -13,7 +13,9 @@ ai-usage: ai-assisted
13
13
14
14
# Quickstart: Azure Blob Storage client library for .NET
15
15
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.
@@ -34,7 +36,7 @@ This section walks you through preparing a project to work with the Azure Blob S
34
36
35
37
### Create the project
36
38
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.
38
40
39
41
### [Visual Studio 2022](#tab/visual-studio)
40
42
@@ -46,7 +48,7 @@ For the steps ahead, you'll need to create a .NET console app using either the .
46
48
47
49
1. For the **Project Name**, enter *BlobQuickstart*. Leave the default values for the rest of the fields and select **Next**.
48
50
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.
If this command to add the package fails, follow these steps:
95
97
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:
97
99
98
100
```dotnetcli
99
101
dotnet nuget list source
100
102
```
101
103
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:
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:
145
145
146
146
## Code examples
147
147
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)
149
156
150
157
> [!IMPORTANT]
151
-
> Make sure you have installed the correct NuGet packages and added the necessary using statements in order forthe code samples to work, as describedin 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.
152
159
153
-
***Azure.Identity** (if you are using the passwordless approach)
Decide on a name forthe 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.
159
165
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:
166
167
167
168
```csharp
168
169
// TODO: Replace <storage-account-name> with your actual storage account name
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).
181
182
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
+
182
186
### Upload a blob to a container
183
187
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
+
184
190
Add the following code to the end of the `Program.cs` class:
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
-
211
211
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with .NET](storage-blob-upload.md).
212
212
213
213
### List blobs in a container
214
214
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.
216
216
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:
218
218
219
219
```csharp
220
220
Console.WriteLine("Listing blobs...");
@@ -230,9 +230,9 @@ To learn more about listing blobs, and to explore more code samples, see [List b
230
230
231
231
### Download a blob
232
232
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 inlocal file system.
234
234
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:
236
236
237
237
```csharp
238
238
// 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
250
250
251
251
### Delete a container
252
252
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.
254
254
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.
256
256
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:
258
258
259
259
```csharp
260
260
// Clean up
@@ -275,7 +275,7 @@ To learn more about deleting a container, and to explore more code samples, see
275
275
276
276
## The completed code
277
277
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:
`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.
20
20
@@ -24,11 +24,11 @@ For example, your app can authenticate using your Visual Studio sign-in credenti
### Assign roles to your Microsoft Entra user account
27
+
####Assign roles to your Microsoft Entra user account
28
28
29
29
[!INCLUDE [assign-roles](assign-roles.md)]
30
30
31
-
### Sign-in and connect your app code to Azure using DefaultAzureCredential
31
+
####Signin and connect your app code to Azure using DefaultAzureCredential
32
32
33
33
You can authorize access to data in your storage account using the following steps:
34
34
@@ -58,7 +58,7 @@ You can authorize access to data in your storage account using the following ste
58
58
> [!NOTE]
59
59
>WhendeployedtoAzure, thissamecodecanbeusedtoauthorizerequeststoAzureStoragefromanapplicationrunninginAzure. 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.
Afteryoucopytheconnectionstring, writeittoanewenvironmentvariableonthelocalmachinerunningtheapplication. Tosettheenvironmentvariable, openaconsolewindow, andfollowtheinstructionsfor your operating system. Replace `<yourconnectionstring>` with your actual connection string.
73
73
@@ -85,9 +85,7 @@ After you add the environment variable in Windows, you must start a new instance
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, andusestheconnectionstringtoconstructaserviceclientobject.
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.
91
89
92
90
Add following code to the end of the `Program.cs` file:
0 commit comments