In this article, we’ll explore how to integrate the Azure Storage client library for Javascript into a Node.js application. We’ll cover the steps to read and write blobs from Azure Storage, and we’ll also discuss how to obtain your Storage Account Key. By the end of this guide, you’ll have javascript code that can interact with Azure Blob Storage.
Table of Contents
Step 1: Install Azure Storage Blob Client Library for Javascript
To add the Azure Storage SDK to your .NET application and perform operations such as reading and writing blobs, add a Azure.Storage.Blobs Nuget package reference to the application:
npm install @azure/storage-blob
Step 2: Create Azure Storage Account and Get Account Key
To authenticate your application with Azure Blob Storage, you will need the Storage Account Key. You can find this key in the Azure Portal under the Security + networking -> Access keys section of your Azure Storage Account settings. This key will be used to authenticate with the Azure Storage Account.

Step 3: Import Azure Storage Blob Client Library
Before the Azure Storage Blob client library for Javascript can be used, the necessary modules need to be imported. This can be done with the following code:
const { BlobServiceClient, StorageSharedKeyCredential, BlobClient } = require('@azure/storage-blob');
Here’s a description of what each module does:
BlobServiceClient– This class provides methods to work with the Blob service, such as creating a container or listing the blobs within a container.StorageSharedKeyCredentials– This class is used for authentication with the storage account using account name and account key.BlobClient– This class represents a client to Azure Storage Blob and provides methods for interacting with an individual blob, such as uploading or downloading data.
By importing these modules, you can create instances of these classes and use them to perform various operations on Azure Blob Storage, such as uploading a blob from a file path, or downloading a blob to a local file path.
Step 4: Write Code to Connect to Azure Blob Storage
To connect to Azure Blob Storage, you’ll need to instantiate a BlobServiceClient using your Storage Account name and key to authenticate.
First, build the connection string for Azure Blob Storage with the following code. To make this easier to read, the account name and key are set to variables that are then put into a connection string using string interpolation.
const storageAccountName = "your_storage_account_name";
const storageAccountKey = "your_storage_account_key";
const sharedKeyCredential = new StorageSharedKeyCredential(storageAccountName, storageAccountKey);
Next, the create a new instance of the Azure.Storage.Blobs.BlobServiceClient class, and pass it the connection string containing the Azure Blob Storage Account credentials in its constructor:
const blobServiceClient = new BlobServiceClient(
`https://${storageAccountName}.blob.core.windows.net`,
sharedKeyCredential
);
Now that the BlobServiceClient has been instantiated, this class can be used to interact with Azure Blob Storage.
Step 5: Create Blob Container Client
All Blobs in an Azure Storage Account are stored within a Blob Container. To work with the blobs in a container, a ContainerClient is needed. Use the following code to create a new ContainerClient for a specific container that’s been created within the Azure Storage Account:
const containerName = "<your_container_name>";
const containerClient = blobServiceClient.getContainerClient(containerName);
If the Blob Container hasn’t been created, it will need to be create before any blobs can be written to it. The following line of code can be used to create the Blob Container if it doesn’t exist yet:
await containerClient.createIfNotExists({ access: 'none' });
The access for the ContainerClient can be set with the following options:
access Option |
Description |
|---|---|
blob |
Public anonymous read access to blobs within the container is allowed, but container data is not available anonymously. |
container |
Public anonymous read access to container and blobs is allowed. Clients can enumerate blobs within the container by anonymous request. |
none |
No anonymous access to any blob within the container is allowed. |
It’s best to always use none unless anonymous access is specifically necessary; for security reasons of course.
Step 6: Create Blob Client
To read or write a blob in the Blob Container, a BlobClient must be created. This can be done using the BlobContainerClient.GetBlobClient passing in the name of the blob:
const blobName = "sample-blob.txt";
const blobClient = containerClient.getBlobClient(blobName);
This same BlobClient can be used to both read and write the blob to the container.
Step 7: Write/Upload File to Azure Blob Storage
To write / upload a file to blob storage, the BlobClient.uploadFile method can be called, passing in a local file path for the file to upload:
const localFilePath = "local-file-path";
const uploadBlobResponse = await blobClient.uploadFile(localFilePath, {
overwrite: true
});
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);
Notice, the second parameter to the .uploadFile method is passing in the argument of overwrite set to true. This tells the BlobClient to overwrite the contents of the blob if it already exists.
Step 8: Read/Download File from Azure Blob Storage
To read / download a blob, the BlobClient.downloadToFile method can be called to download the contents of the blob to the specified local file path:
const downloadFilePath = "download-file-path";
const downloadBlockBlobResponse = await blobClient.downloadToFile(downloadFilePath);
console.log(`Downloaded blob content to ${downloadFilePath}`, downloadBlockBlobResponse.requestId);
Conclusion
You’ve now successfully integrated the Azure Storage Blob client library for Javascript into your Node.js application and can read and write blobs to Azure Storage. With these basic operations, you can build upon this foundation to create more complex applications that leverage the power of Azure Blob Storage.
Original Article Source: Read and Write Azure Blob Storage with Javascript written by Chris Pietschmann (If you're reading this somewhere other than Build5Nines.com, it was republished without permission.)
Microsoft Azure Regions: Interactive Map of Global Datacenters
Create Azure Architecture Diagrams with Microsoft Visio
Byte Conversion Calculator from KB, MB, GB, TB, PB
Azure Functions: Extend Execution Timeout Past 5 Minutes
IPv4 Address CIDR Range Reference and Calculator





