Skip to content

Commit bb202ec

Browse files
progress
1 parent bbf56e0 commit bb202ec

File tree

1 file changed

+175
-25
lines changed

1 file changed

+175
-25
lines changed

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

Lines changed: 175 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ title: "Quickstart: Azure Blob storage library - JavaScript"
33
description: In this quickstart, you learn how to use the Azure Blob Storage for JavaScript 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: 03/05/2024
6+
ms.date: 03/06/2024
77
ms.service: azure-blob-storage
88
ms.topic: quickstart
99
ms.devlang: javascript
@@ -27,18 +27,48 @@ zone_pivot_groups: azure-blob-storage-quickstart-options
2727
2828
::: zone-end
2929

30-
Get started with the Azure Blob Storage client library for Node.js to manage blobs and containers. Follow these steps to install the package and try out example code for basic tasks.
30+
Get started with the Azure Blob Storage client library for Node.js to manage blobs and containers.
31+
32+
::: zone pivot="blob-storage-quickstart-scratch"
33+
34+
In this article, you follow steps to install the package and try out example code for basic tasks.
35+
36+
::: zone-end
37+
38+
::: zone pivot="blob-storage-quickstart-template"
39+
40+
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.
41+
42+
::: zone-end
3143

3244
[API reference](/javascript/api/@azure/storage-blob) |
3345
[Library source code](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/storage/storage-blob) | [Package (npm)](https://www.npmjs.com/package/@azure/storage-blob) | [Samples](../common/storage-samples-javascript.md?toc=/azure/storage/blobs/toc.json#blob-samples)
3446

3547
## Prerequisites
3648

49+
::: zone pivot="blob-storage-quickstart-scratch"
50+
3751
- Azure account with an active subscription - [create an account for free](https://azure.microsoft.com/free/?ref=microsoft.com&utm_source=microsoft.com&utm_medium=docs&utm_campaign=visualstudio)
3852
- Azure Storage account - [Create a storage account](../common/storage-account-create.md)
3953
- [Node.js LTS](https://nodejs.org/en/download/)
4054

41-
## Create the Node.js project
55+
::: zone-end
56+
57+
::: zone pivot="blob-storage-quickstart-template"
58+
59+
- Azure subscription - [create one for free](https://azure.microsoft.com/free/)
60+
- [Node.js LTS](https://nodejs.org/en/download/)
61+
- [Azure Developer CLI](/azure/developer/azure-developer-cli/install-azd)
62+
63+
::: zone-end
64+
65+
## Setting up
66+
67+
::: zone pivot="blob-storage-quickstart-scratch"
68+
69+
This section walks you through preparing a project to work with the Azure Blob Storage client library for Node.js.
70+
71+
### Create the Node.js project
4272

4373
Create a JavaScript application named *blob-quickstart*.
4474

@@ -66,7 +96,7 @@ Create a JavaScript application named *blob-quickstart*.
6696
code .
6797
```
6898

69-
## Install the packages
99+
### Install the packages
70100

71101
From the project directory, install the following packages using the `npm install` command.
72102

@@ -88,7 +118,7 @@ From the project directory, install the following packages using the `npm instal
88118
npm install uuid dotenv
89119
```
90120

91-
## Create JavaScript file
121+
### Create JavaScript file
92122

93123
From the project directory:
94124

@@ -97,6 +127,65 @@ From the project directory:
97127

98128
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/boilerplate.js" :::
99129

130+
::: zone-end
131+
132+
::: zone pivot="blob-storage-quickstart-template"
133+
134+
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).
135+
136+
### Initialize the Azure Developer CLI template and deploy resources
137+
138+
From an empty directory, follow these steps to initialize the `azd` template, provision Azure resources, and get started with the code:
139+
140+
- Clone the quickstart repository assets from GitHub and initialize the template locally:
141+
142+
```console
143+
azd init --template blob-storage-quickstart-nodejs
144+
```
145+
146+
You'll be prompted for the following information:
147+
148+
- **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.
149+
150+
- Log in to Azure:
151+
152+
```console
153+
azd auth login
154+
```
155+
- Provision and deploy the resources to Azure:
156+
157+
```console
158+
azd up
159+
```
160+
161+
You'll be prompted for the following information:
162+
163+
- **Subscription**: The Azure subscription that your resources are deployed to.
164+
- **Location**: The Azure region where your resources are deployed.
165+
166+
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.
167+
168+
## Run the sample code
169+
170+
At this point, the resources are deployed to Azure and the code is almost ready to run. Follow these steps to install packages, update the name of the storage account in the code, and run the sample console app:
171+
172+
1. **Install packages**: Navigate to the local `blob-quickstart` directory. Install packages for the Azure Blob Storage and Azure Identity client libraries, along with other packages used in the quickstart, using the following command:
173+
```console
174+
npm install @azure/storage-blob @azure/identity uuid dotenv
175+
```
176+
1. **Update the storage account name**: In the local `blob-quickstart` directory, edit the file named **index.js**. 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.
177+
1. **Run the project**: Execute the following command to run the app:
178+
```console
179+
node index.js
180+
```
181+
1. **Observe the output**: This app creates a container and uploads a text string as a blob to the container. The example then lists the blobs in the container and downloads the blob and displays the blob contents. The app then deletes the container and all its blobs.
182+
183+
To learn more about how the sample code works, see [Code examples](#code-examples).
184+
185+
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.
186+
187+
::: zone-end
188+
100189
## Object model
101190

102191
Azure Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that doesn't adhere to a particular data model or definition, such as text or binary data. Blob storage offers three types of resources:
@@ -127,8 +216,19 @@ These example code snippets show you how to do the following tasks with the Azur
127216
- [Download blobs](#download-blobs)
128217
- [Delete a container](#delete-a-container)
129218

219+
::: zone pivot="blob-storage-quickstart-scratch"
220+
130221
Sample code is also available on [GitHub](https://github.com/Azure-Samples/AzureStorageSnippets/tree/master/blobs/quickstarts/JavaScript/V12/nodejs).
131222

223+
::: zone-end
224+
225+
::: zone pivot="blob-storage-quickstart-template"
226+
227+
> [!NOTE]
228+
> The Azure Developer CLI template includes a file 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.
229+
230+
::: zone-end
231+
132232
### Authenticate to Azure and authorize access to blob data
133233

134234
[!INCLUDE [storage-quickstart-passwordless-auth-intro](../../../includes/storage-quickstart-passwordless-auth-intro.md)]
@@ -231,8 +331,12 @@ AZURE_STORAGE_CONNECTION_STRING="<yourconnectionstring>"
231331

232332
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.
233333

334+
::: zone pivot="blob-storage-quickstart-scratch"
335+
234336
Add this code inside the `try` block:
235337

338+
::: zone-end
339+
236340
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_StorageAcctInfo__with_secrets":::
237341

238342
> [!IMPORTANT]
@@ -242,63 +346,91 @@ Add this code inside the `try` block:
242346

243347
## Create a container
244348

245-
1. Decide on a name for the new container. Container names must be lowercase.
349+
Create a new container in the storage account. The following code example takes a [BlobServiceClient](/javascript/api/@azure/storage-blob/blobserviceclient) object and calls the [getContainerClient](/javascript/api/@azure/storage-blob/blobserviceclient#getcontainerclient-string-) method to get a reference to a container. Then, the code calls the [create](/javascript/api/@azure/storage-blob/containerclient#create-containercreateoptions-) method to actually create the container in your storage account.
246350

247-
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).
351+
::: zone pivot="blob-storage-quickstart-scratch"
248352

249-
1. Add this code to the end of the `main` function:
353+
Add this code to the end of the `try` block:
250354

251-
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_CreateContainer":::
355+
::: zone-end
252356

253-
The preceding code takes a [BlobServiceClient](/javascript/api/@azure/storage-blob/blobserviceclient) object and calls the [getContainerClient](/javascript/api/@azure/storage-blob/blobserviceclient#getcontainerclient-string-) method to get a reference to a container. Finally, the code calls [create](/javascript/api/@azure/storage-blob/containerclient#create-containercreateoptions-) to actually create the container in your storage account.
357+
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_CreateContainer":::
254358

255359
To learn more about creating a container, and to explore more code samples, see [Create a blob container with JavaScript](storage-blob-container-create-javascript.md).
256360

361+
> [!IMPORTANT]
362+
> 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).
363+
257364
## Upload blobs to a container
258365

259-
Copy the following code to the end of the `main` function to upload a text string to a blob:
366+
Upload a blob to the container. The following code gets a reference to a [BlockBlobClient](/javascript/api/@azure/storage-blob/blockblobclient) object by calling the [getBlockBlobClient](/javascript/api/@azure/storage-blob/containerclient#getblockblobclient-string-) method on the [ContainerClient](/javascript/api/@azure/storage-blob/containerclient) from the [Create a container](#create-a-container) section.
260367

261-
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_UploadBlobs":::
262-
263-
The preceding code gets a reference to a [BlockBlobClient](/javascript/api/@azure/storage-blob/blockblobclient) object by calling the [getBlockBlobClient](/javascript/api/@azure/storage-blob/containerclient#getblockblobclient-string-) method on the [ContainerClient](/javascript/api/@azure/storage-blob/containerclient) from the [Create a container](#create-a-container) section.
264368
The code uploads the text string data to the blob by calling the [upload](/javascript/api/@azure/storage-blob/blockblobclient#upload-httprequestbody--number--blockblobuploadoptions-) method.
265369

370+
::: zone pivot="blob-storage-quickstart-scratch"
371+
372+
Add this code to the end of the `try` block:
373+
374+
::: zone-end
375+
376+
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_UploadBlobs":::
377+
266378
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with JavaScript](storage-blob-upload-javascript.md).
267379

268380
## List the blobs in a container
269381

270-
Add the following code to the end of the `main` function to list the blobs in the container.
382+
List the blobs in the container. The following code calls the [listBlobsFlat](/javascript/api/@azure/storage-blob/containerclient#listblobsflat-containerlistblobsoptions-) method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob.
271383

272-
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_ListBlobs":::
384+
::: zone pivot="blob-storage-quickstart-scratch"
273385

274-
The preceding code calls the [listBlobsFlat](/javascript/api/@azure/storage-blob/containerclient#listblobsflat-containerlistblobsoptions-) method. In this case, only one blob has been added to the container, so the listing operation returns just that one blob.
386+
Add this code to the end of the `try` block:
387+
388+
::: zone-end
389+
390+
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_ListBlobs":::
275391

276392
To learn more about listing blobs, and to explore more code samples, see [List blobs with JavaScript](storage-blobs-list-javascript.md).
277393

278394
## Download blobs
279395

280-
1. Add the following code to the end of the `main` function to download the previously created blob into the app runtime.
396+
Download the blob and display the contents. The following code calls the [download](/javascript/api/@azure/storage-blob/blockblobclient#download-undefined---number--undefined---number--blobdownloadoptions-) method to download the blob.
281397

282-
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_DownloadBlobs":::
398+
::: zone pivot="blob-storage-quickstart-scratch"
283399

284-
The preceding code calls the [download](/javascript/api/@azure/storage-blob/blockblobclient#download-undefined---number--undefined---number--blobdownloadoptions-) method.
400+
Add this code to the end of the `try` block:
285401

286-
2. Copy the following code *after* the `main` function to convert a stream back into a string.
402+
::: zone-end
403+
404+
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_DownloadBlobs":::
405+
406+
The following code converts a stream back into a string to display the contents.
407+
408+
::: zone pivot="blob-storage-quickstart-scratch"
409+
410+
Add this code *after* the `main` function:
411+
412+
::: zone-end
287413

288-
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_ConvertStreamToText":::
414+
:::code language="javascript" source="~/azure_storage-snippets/blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_ConvertStreamToText":::
289415

290416
To learn more about downloading blobs, and to explore more code samples, see [Download a blob with JavaScript](storage-blob-download-javascript.md).
291417

292418
## Delete a container
293419

294-
Add this code to the end of the `main` function to delete the container and all its blobs:
420+
Delete the container and all blobs within the container. The following code cleans up the resources created by the app by removing the entire container using the [​delete](/javascript/api/@azure/storage-blob/containerclient#delete-containerdeletemethodoptions-) method.
295421

296-
:::code language="javascript" source="~/azure_storage-snippets//blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_DeleteContainer":::
422+
::: zone pivot="blob-storage-quickstart-scratch"
297423

298-
The preceding code cleans up the resources the app created by removing the entire container using the [​delete](/javascript/api/@azure/storage-blob/containerclient#delete-containerdeletemethodoptions-) method. You can also delete the local files, if you like.
424+
Add this code to the end of the `try` block:
425+
426+
::: zone-end
427+
428+
:::code language="javascript" source="~/azure_storage-snippets//blobs/quickstarts/JavaScript/V12/nodejs/index.js" id="snippet_DeleteContainer":::
299429

300430
To learn more about deleting a container, and to explore more code samples, see [Delete and restore a blob container with JavaScript](storage-blob-container-delete-javascript.md).
301431

432+
::: zone pivot="blob-storage-quickstart-scratch"
433+
302434
## Run the code
303435

304436
1. From a Visual Studio Code terminal, run the app.
@@ -330,11 +462,29 @@ To learn more about deleting a container, and to explore more code samples, see
330462

331463
Step through the code in your debugger and check your [Azure portal](https://portal.azure.com) throughout the process. Check to see that the container is being created. You can open the blob inside the container and view the contents.
332464

465+
::: zone-end
466+
333467
## Clean up
334468

469+
::: zone pivot="blob-storage-quickstart-scratch"
470+
335471
1. When you're done with this quickstart, delete the `blob-quickstart` directory.
336472
1. If you're done using your Azure Storage resource, use the [Azure CLI to remove the Storage resource](storage-quickstart-blobs-cli.md#clean-up-resources).
337473

474+
::: zone-end
475+
476+
::: zone pivot="blob-storage-quickstart-template"
477+
478+
When you're done with the quickstart, you can clean up the resources you created by running the following command:
479+
480+
```console
481+
azd down
482+
```
483+
484+
You'll be prompted to confirm the deletion of the resources. Enter `y` to confirm.
485+
486+
::: zone-end
487+
338488
## Next steps
339489

340490
In this quickstart, you learned how to upload, download, and list blobs using JavaScript.

0 commit comments

Comments
 (0)