Azure SDK for JavaScript

Interact with Azure Blob Storage seamlessly

Getting Started with Azure Blob Storage SDK for JavaScript

This guide will walk you through the essential steps to start using the Azure Blob Storage SDK for JavaScript to manage your cloud storage.

1. Prerequisites

Before you begin, ensure you have the following:

  • An Azure account.
  • An Azure Storage account. You can create one in the Azure portal.
  • Node.js installed on your development machine (version 12 or later recommended).
  • A package manager like npm or yarn.

2. Installation

Install the Azure Blob Storage client library for JavaScript using npm:

npm install @azure/storage-blob

3. Authentication

To authenticate with your Azure Blob Storage account, you can use either connection strings or Azure Active Directory (AAD) credentials. For simplicity in this guide, we'll use a connection string.

Using a Connection String

Obtain your storage account connection string from the Azure portal under "Access keys".

// Example of a connection string (do not expose in client-side code)
const connectionString = "DefaultEndpointsProtocol=https;AccountName=YOUR_ACCOUNT_NAME;AccountKey=YOUR_ACCOUNT_KEY;EndpointSuffix=core.windows.net";

4. Creating a BlobServiceClient

The BlobServiceClient is the main entry point for interacting with Blob Storage. You can create it using your connection string.

import { BlobServiceClient } from "@azure/storage-blob"; async function getBlobServiceClient(connectionString) { const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString); return blobServiceClient; } // Usage: // const connectionString = "YOUR_CONNECTION_STRING"; // Replace with your actual connection string // getBlobServiceClient(connectionString).then(client => { // console.log("BlobServiceClient created successfully."); // // Now you can use 'client' to interact with your storage. // }).catch(error => { // console.error("Error creating BlobServiceClient:", error); // });

5. Working with Containers

Containers are the top-level organization units within a storage account. You can create, list, and delete containers.

Creating a Container

To create a container, you'll first get a ContainerClient from the BlobServiceClient.

import { BlobServiceClient } from "@azure/storage-blob"; async function createContainer(blobServiceClient, containerName) { try { const containerClient = blobServiceClient.getContainerClient(containerName); await containerClient.createIfNotExists(); console.log(`Container "${containerName}" created or already exists.`); return containerClient; } catch (error) { console.error("Error creating container:", error); throw error; } } // Usage: // const blobServiceClient = ...; // Obtained from previous step // const newContainerName = "my-sample-container"; // createContainer(blobServiceClient, newContainerName).then(containerClient => { // console.log(`Container client for "${newContainerName}" obtained.`); // }).catch(error => { // console.error("Failed to create container."); // });

6. Next Steps

Now that you have a basic setup, you can explore other functionalities: