Skip to content

Commit bde8362

Browse files
Merge pull request #264531 from pauljewellmsft/azd-blob-dotnet-qs
Add azd support to .NET quickstart for blobs
2 parents 5762059 + 0975acd commit bde8362

File tree

2 files changed

+162
-5
lines changed

2 files changed

+162
-5
lines changed

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

Lines changed: 153 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,35 +3,78 @@ title: "Quickstart: Azure Blob Storage library - .NET"
33
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: 01/30/2024
6+
ms.date: 02/06/2024
77
ms.service: azure-blob-storage
88
ms.topic: quickstart
99
ms.devlang: csharp
1010
ms.custom: devx-track-csharp, mode-api, passwordless-dotnet, devx-track-dotnet, ai-video-demo
1111
ai-usage: ai-assisted
12+
zone_pivot_groups: azure-blob-storage-quickstart-options
1213
---
1314

1415
# Quickstart: Azure Blob Storage client library for .NET
1516

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+
1631
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.
1732

33+
::: zone pivot="blob-storage-quickstart-scratch"
34+
1835
In this article, you follow steps to install the package and try out example code for basic tasks.
1936

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.
42+
43+
::: zone-end
44+
2045
[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)
2146

47+
::: zone pivot="blob-storage-quickstart-scratch"
48+
2249
This video shows you how to start using the Azure Blob Storage client library for .NET.
2350
> [!VIDEO cdae65e7-1892-48fe-934a-70edfbe147be]
2451
2552
The steps in the video are also described in the following sections.
2653

54+
::: zone-end
55+
2756
## Prerequisites
2857

58+
::: zone pivot="blob-storage-quickstart-scratch"
59+
2960
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
3061
- 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.
70+
- [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd)
71+
72+
::: zone-end
3273

3374
## Setting up
3475

76+
::: zone pivot="blob-storage-quickstart-scratch"
77+
3578
This section walks you through preparing a project to work with the Azure Blob Storage client library for .NET.
3679

3780
### Create the project
@@ -125,6 +168,58 @@ using System.IO;
125168
Console.WriteLine("Hello, World!");
126169
```
127170
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:
182+
183+
```console
184+
azd init --template blob-storage-quickstart-dotnet
185+
```
186+
187+
You'll be prompted for the following information:
188+
189+
- **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+
128223
## Object model
129224
130225
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
154249
- [Download a blob](#download-a-blob)
155250
- [Delete a container](#delete-a-container)
156251

252+
::: zone pivot="blob-storage-quickstart-scratch"
253+
157254
> [!IMPORTANT]
158255
> 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.
159256
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.
263+
264+
::: zone-end
265+
160266
[!INCLUDE [storage-quickstart-credential-free-include](../../../includes/storage-quickstart-credential-free-include.md)]
161267
162268
### Create a container
163269
164270
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.
165271

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
167277

168278
```csharp
169279
// 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
187297
188298
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.
189299
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
191305
192306
```csharp
193307
// 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
214328
215329
List the blobs in the container by calling the [GetBlobsAsync](/dotnet/api/azure.storage.blobs.blobcontainerclient.getblobsasync) method.
216330
331+
::: zone pivot="blob-storage-quickstart-scratch"
332+
217333
Add the following code to the end of the `Program.cs` file:
218334
335+
::: zone-end
336+
219337
```csharp
220338
Console.WriteLine("Listing blobs...");
221339
@@ -232,8 +350,12 @@ To learn more about listing blobs, and to explore more code samples, see [List b
232350
233351
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.
234352
353+
::: zone pivot="blob-storage-quickstart-scratch"
354+
235355
Add the following code to the end of the `Program.cs` file:
236356
357+
::: zone-end
358+
237359
```csharp
238360
// Download the blob to a local file
239361
// 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
254376
255377
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.
256378
379+
::: zone pivot="blob-storage-quickstart-scratch"
380+
257381
Add the following code to the end of the `Program.cs` file:
258382
383+
::: zone-end
384+
259385
```csharp
260386
// Clean up
261387
Console.Write("Press any key to begin clean up");
@@ -273,6 +399,8 @@ Console.WriteLine("Done");
273399
274400
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).
275401
402+
::: zone pivot="blob-storage-quickstart-scratch"
403+
276404
## The completed code
277405
278406
After completing these steps, the code in your `Program.cs` file should now resemble the following:
@@ -446,7 +574,27 @@ Done
446574
447575
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.
448576
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.
596+
597+
::: zone-end
450598
451599
## Next steps
452600

articles/zone-pivot-groups.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2999,3 +2999,12 @@ groups:
29992999
title: TypeScript
30003000
- id: programming-language-portal
30013001
title: Azure Portal
3002+
# Owner: pauljewell
3003+
- id: azure-blob-storage-quickstart-options
3004+
title: Quickstart interaction type
3005+
prompt: Choose how you'd like to interact with the quickstart
3006+
pivots:
3007+
- id: blob-storage-quickstart-scratch
3008+
title: Build from scratch
3009+
- id: blob-storage-quickstart-template
3010+
title: Start with a template

0 commit comments

Comments
 (0)