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 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.
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.
- 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)
38
52
- Azure Storage account - [Create a storage account](../common/storage-account-create.md)
39
53
-[Node.js LTS](https://nodejs.org/en/download/)
40
54
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/)
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:
- **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:
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
+
100
189
## Object model
101
190
102
191
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
127
216
- [Download blobs](#download-blobs)
128
217
- [Delete a container](#delete-a-container)
129
218
219
+
::: zone pivot="blob-storage-quickstart-scratch"
220
+
130
221
Sample code is also available on [GitHub](https://github.com/Azure-Samples/AzureStorageSnippets/tree/master/blobs/quickstarts/JavaScript/V12/nodejs).
131
222
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 foreach part of the sample code. The template implements the recommended passwordless authentication method, as describedin 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
+
132
232
### Authenticate to Azure and authorize access to blob data
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.
@@ -242,63 +346,91 @@ Add this code inside the `try` block:
242
346
243
347
## Create a container
244
348
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.
246
350
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"
248
352
249
-
1.Add this code to the end of the `main` function:
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.
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).
256
360
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
+
257
364
## Upload blobs to a container
258
365
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.
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.
264
368
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.
To learn more about uploading blobs, and to explore more code samples, see [Upload a blob with JavaScript](storage-blob-upload-javascript.md).
267
379
268
380
## List the blobs in a container
269
381
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.
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.
To learn more about listing blobs, and to explore more code samples, see [List blobs with JavaScript](storage-blobs-list-javascript.md).
277
393
278
394
## Download blobs
279
395
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.
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:
285
401
286
-
2. Copy the following code *after* the `main` function to convert a stream back into a string.
To learn more about downloading blobs, and to explore more code samples, see [Download a blob with JavaScript](storage-blob-download-javascript.md).
291
417
292
418
## Delete a container
293
419
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.
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.
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).
301
431
432
+
::: zone pivot="blob-storage-quickstart-scratch"
433
+
302
434
## Run the code
303
435
304
436
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
330
462
331
463
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.
332
464
465
+
::: zone-end
466
+
333
467
## Clean up
334
468
469
+
::: zone pivot="blob-storage-quickstart-scratch"
470
+
335
471
1. When you're done with this quickstart, delete the `blob-quickstart` directory.
336
472
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).
337
473
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
+
338
488
## Next steps
339
489
340
490
In this quickstart, you learned how to upload, download, and list blobs using JavaScript.
0 commit comments