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 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.
# Quickstart: Azure Blob Storage client library for .NET
15
16
17
+
::: zone pivot="blob-storage-quickstart-scratch"
18
+
19
+
> [!NOTE]
20
+
> The **Build from scratch** option walks you step by step through the process of creating a new project, installing packages, writing the code, and running a basic console app. This approach is recommended if you want to understand all the details involved in creating an app that connects to Azure Blob Storage. If you prefer to automate deployment tasks and start with a completed project, choose [Start with a template](storage-quickstart-blobs-dotnet.md?pivots=blob-storage-quickstart-template).
21
+
22
+
::: zone-end
23
+
24
+
::: zone pivot="blob-storage-quickstart-template"
25
+
26
+
> [!NOTE]
27
+
> The **Start with a template** option uses the Azure Developer CLI to automate deployment tasks and starts you off with a completed project. This approach is recommended if you want to explore the code as quickly as possible without going through the setup tasks. If you prefer step by step instructions to build the app, choose [Build from scratch](storage-quickstart-blobs-dotnet.md?pivots=blob-storage-quickstart-scratch).
28
+
29
+
::: zone-end
30
+
16
31
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
32
33
+
::: zone pivot="blob-storage-quickstart-scratch"
34
+
18
35
In this article, you follow steps to install the package and try out example code for basic tasks.
19
36
37
+
::: zone-end
38
+
39
+
::: zone pivot="blob-storage-quickstart-template"
40
+
41
+
In this article, you use the [Azure Developer CLI](/azure/developer/azure-developer-cli/overview) to deploy Azure resources and run a completed console app with just a few commands.
This video shows you how to start using the Azure Blob Storage client library for .NET.
23
50
> [!VIDEO cdae65e7-1892-48fe-934a-70edfbe147be]
24
51
25
52
The steps in the video are also described in the following sections.
26
53
54
+
::: zone-end
55
+
27
56
## Prerequisites
28
57
58
+
::: zone pivot="blob-storage-quickstart-scratch"
59
+
29
60
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
30
61
- Azure storage account - [create a storage account](../common/storage-account-create.md)
31
-
- Current [.NET SDK](https://dotnet.microsoft.com/download/dotnet) for your operating system. Be sure to get the SDK and not the runtime.
62
+
- Latest [.NET SDK](https://dotnet.microsoft.com/download/dotnet) for your operating system. Be sure to get the SDK and not the runtime.
63
+
64
+
::: zone-end
65
+
66
+
::: zone pivot="blob-storage-quickstart-template"
67
+
68
+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
69
+
- Latest [.NET SDK](https://dotnet.microsoft.com/download/dotnet) for your operating system. This code sample uses .NET 8.0. Be sure to get the SDK and not the runtime.
This section walks you through preparing a project to work with the Azure Blob Storage client library for .NET.
36
79
37
80
### Create the project
@@ -125,6 +168,58 @@ using System.IO;
125
168
Console.WriteLine("Hello, World!");
126
169
```
127
170
171
+
::: zone-end
172
+
173
+
::: zone pivot="blob-storage-quickstart-template"
174
+
175
+
With [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd) installed, you can create a storage account and run the sample code with just a few commands. You can run the project in your local development environment, or in a [DevContainer](https://code.visualstudio.com/docs/devcontainers/containers).
176
+
177
+
### Initialize the Azure Developer CLI template and deploy resources
178
+
179
+
From an empty directory, follow these steps to initialize the `azd` template, provision Azure resources, and get started with the code:
180
+
181
+
- Clone the quickstart repository assets from GitHub and initialize the template locally:
- **Environment name**: This value is used as a prefix for all Azure resources created by Azure Developer CLI. The name must be unique across all Azure subscriptions and must be between 3 and 24 characters long. The name can contain numbers and lowercase letters only.
190
+
191
+
- Log in to Azure:
192
+
193
+
```console
194
+
azd auth login
195
+
```
196
+
- Provision and deploy the resources to Azure:
197
+
198
+
```console
199
+
azd up
200
+
```
201
+
202
+
You'll be prompted for the following information:
203
+
204
+
- **Subscription**: The Azure subscription that your resources are deployed to.
205
+
- **Location**: The Azure region where your resources are deployed.
206
+
207
+
The deployment might take a few minutes to complete. The output from the `azd up` command includes the name of the newly created storage account, which you'll need later to run the code.
208
+
209
+
## Run the sample code
210
+
211
+
At this point, the resources are deployed to Azure and the project is ready to run. Follow these steps to update the name of the storage account in the code and run the sample console app:
212
+
213
+
- **Update the storage account name**: Navigate to the `src` directory and edit `Program.cs`. Find the `<storage-account-name>` placeholder and replace it with the actual name of the storage account created by the `azd up` command. Save the changes.
214
+
- **Run the project**: If you're using Visual Studio, press F5 to build and run the code and interact with the console app. If you're using the .NET CLI, navigate to your application directory, build the project using `dotnet build`, and run the application using the `dotnet run`.
215
+
- **Observe the output**: This app creates a test file in your local*data* folder and uploads it to a container in the storage account. The example then lists the blobs in the container and downloads the file with a new name so that you can compare the old and new files.
216
+
217
+
To learn more about how the sample code works, see [Code examples](#code-examples).
218
+
219
+
When you're finished testing the code, see the [Clean up resources](#clean-up-resources) section to delete the resources created by the `azd up` command.
220
+
221
+
::: zone-end
222
+
128
223
## Object model
129
224
130
225
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:
@@ -154,16 +249,31 @@ The sample code snippets in the following sections demonstrate how to perform th
154
249
- [Download a blob](#download-a-blob)
155
250
- [Delete a container](#delete-a-container)
156
251
252
+
::: zone pivot="blob-storage-quickstart-scratch"
253
+
157
254
> [!IMPORTANT]
158
255
> 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.
159
256
257
+
::: zone-end
258
+
259
+
::: zone pivot="blob-storage-quickstart-template"
260
+
261
+
> [!NOTE]
262
+
> The Azure Developer CLI template includes a project with sample code already in place. The following examples provide detail for each part of the sample code. The template implements the recommended passwordless authentication method, as described in the [Authenticate to Azure](#authenticate-to-azure-and-authorize-access-to-blob-data) section. The connection string method is shown as an alternative, but isn't used in the template and isn't recommended for production code.
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.
165
271
166
-
Add this code to the end of the `Program.cs` file:
272
+
::: zone pivot="blob-storage-quickstart-scratch"
273
+
274
+
Add the following code to the end of the `Program.cs` file:
275
+
276
+
::: zone-end
167
277
168
278
```csharp
169
279
// TODO: Replace <storage-account-name> with your actual storage account name
@@ -187,7 +297,11 @@ To learn more about creating a container, and to explore more code samples, see
187
297
188
298
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
299
190
-
Add the following code to the end of the `Program.cs` class:
300
+
::: zone pivot="blob-storage-quickstart-scratch"
301
+
302
+
Add the following code to the end of the `Program.cs` file:
303
+
304
+
::: zone-end
191
305
192
306
```csharp
193
307
// Create a local file in the ./data/ directory for uploading and downloading
@@ -214,8 +328,12 @@ To learn more about uploading blobs, and to explore more code samples, see [Uplo
214
328
215
329
List the blobs in the container by calling the [GetBlobsAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobsasync) method.
216
330
331
+
::: zone pivot="blob-storage-quickstart-scratch"
332
+
217
333
Add the following code to the end of the `Program.cs` file:
218
334
335
+
::: zone-end
336
+
219
337
```csharp
220
338
Console.WriteLine("Listing blobs...");
221
339
@@ -232,8 +350,12 @@ To learn more about listing blobs, and to explore more code samples, see [List b
232
350
233
351
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
352
353
+
::: zone pivot="blob-storage-quickstart-scratch"
354
+
235
355
Add the following code to the end of the `Program.cs` file:
236
356
357
+
::: zone-end
358
+
237
359
```csharp
238
360
// Download the blob to a local file
239
361
// Append the string "DOWNLOADED" before the .txt extension
@@ -254,8 +376,12 @@ The following code cleans up the resources the app created by deleting the conta
254
376
255
377
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
378
379
+
::: zone pivot="blob-storage-quickstart-scratch"
380
+
257
381
Add the following code to the end of the `Program.cs` file:
258
382
383
+
::: zone-end
384
+
259
385
```csharp
260
386
// Clean up
261
387
Console.Write("Press any key to begin clean up");
@@ -273,6 +399,8 @@ Console.WriteLine("Done");
273
399
274
400
To learn more about deleting a container, and to explore more code samples, see [Delete and restore a blob container with .NET](storage-blob-container-delete.md).
275
401
402
+
::: zone pivot="blob-storage-quickstart-scratch"
403
+
276
404
## The completed code
277
405
278
406
After completing these steps, the code in your `Program.cs` file should now resemble the following:
@@ -446,7 +574,27 @@ Done
446
574
447
575
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.
448
576
449
-
After you verify the files, press the **Enter** key to delete the test files and finish the demo.
577
+
::: zone-end
578
+
579
+
## Clean up resources
580
+
581
+
::: zone pivot="blob-storage-quickstart-scratch"
582
+
583
+
After you verify the files and finish testing, press the **Enter** key to delete the test files along with the container you created in the storage account. You can also use [Azure CLI](storage-quickstart-blobs-cli.md#clean-up-resources) to delete resources.
584
+
585
+
::: zone-end
586
+
587
+
::: zone pivot="blob-storage-quickstart-template"
588
+
589
+
When you're done with the quickstart, you can clean up the resources you created by running the following command:
590
+
591
+
```console
592
+
azd down
593
+
```
594
+
595
+
You'll be prompted to confirm the deletion of the resources. Enter `y` to confirm.
0 commit comments