Blob Storage
Azure Blob Storage is Microsoft's object storage solution for the cloud. Blob storage is optimized to store massive amounts of unstructured data, such as text or binary data.
The Azure Blob Storage client library for JavaScript allows you to manage and interact with blob data in your Azure Storage account. This documentation will guide you through the core functionalities and best practices.
Getting Started
To get started, you'll need an Azure Storage account. You can create one through the Azure portal. Once you have your account, you'll need your connection string or account name and a shared access signature (SAS) token.
Install the SDK using npm or yarn:
npm install @azure/storage-blob
# or
yarn add @azure/storage-blob
Here's a basic example of how to initialize a BlobServiceClient:
Example: Initializing BlobServiceClient
import { BlobServiceClient } from "@azure/storage-blob";
const connectionString = ""; // Or use accountName and accountKey/SAS token
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
console.log("BlobServiceClient initialized successfully.");
Core Concepts
The Azure Blob Storage SDK for JavaScript is built around a few key client objects:
BlobServiceClient
The BlobServiceClient is the primary entry point for interacting with Azure Blob Storage. It allows you to perform operations at the account level, such as creating or accessing containers.
ContainerClient
The ContainerClient represents a specific blob container within your storage account. You can use it to perform operations related to a container, such as creating, deleting, or listing blobs within it.
BlobClient
The BlobClient represents a single blob within a container. It provides methods for uploading, downloading, deleting, and managing the properties of a specific blob.
Blob Operations
Uploading Blobs
You can upload various types of data as blobs, including text, binary data, and file streams. The SDK supports uploading blocks blobs, append blobs, and page blobs.
Example: Uploading a Text Blob
async function uploadTextBlob(containerName, blobName, textData) {
const containerClient = blobServiceClient.getContainerClient(containerName);
const blockBlobClient = containerClient.getBlockBlobClient(blobName);
await blockBlobClient.upload(textData, textData.length);
console.log(`Blob "${blobName}" uploaded successfully.`);
}
// Usage:
// uploadTextBlob("my-container", "my-text-blob.txt", "Hello, Azure Blob Storage!");
Downloading Blobs
Download blob content to your application. You can download as a string, an array buffer, or a stream.
Example: Downloading a Blob as Text
async function downloadBlobAsText(containerName, blobName) {
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
const downloadResponse = await blobClient.downloadToBuffer(0);
const blobContent = downloadResponse.toString();
console.log(`Blob "${blobName}" content:`, blobContent);
return blobContent;
}
// Usage:
// downloadBlobAsText("my-container", "my-text-blob.txt");
Deleting Blobs
Remove a blob from a container.
Example: Deleting a Blob
async function deleteBlob(containerName, blobName) {
const containerClient = blobServiceClient.getContainerClient(containerName);
const blobClient = containerClient.getBlobClient(blobName);
await blobClient.delete();
console.log(`Blob "${blobName}" deleted successfully.`);
}
// Usage:
// deleteBlob("my-container", "my-text-blob.txt");
Listing Blobs
Retrieve a list of all blobs within a container.
Example: Listing Blobs in a Container
async function listBlobs(containerName) {
const containerClient = blobServiceClient.getContainerClient(containerName);
let blobs = [];
for await (const blobItem of containerClient.listBlobs()) {
blobs.push(blobItem.name);
console.log("- ", blobItem.name);
}
return blobs;
}
// Usage:
// listBlobs("my-container");
Managing Blob Properties
Get and set metadata and system properties for blobs.
Example: Setting Blob Metadata
async function setBlobMetadata(containerName, blobName, metadata) {
const blobClient = blobServiceClient.getContainerClient(containerName).getBlobClient(blobName);
await blobClient.setMetadata(metadata);
console.log(`Metadata set for blob "${blobName}".`);
}
// Usage:
// setBlobMetadata("my-container", "my-text-blob.txt", { author: "John Doe", version: "1.0" });
Managing Blob Tags
Apply and query tags on blobs for resource management and cost analysis.
Example: Setting Blob Tags
async function setBlobTags(containerName, blobName, tags) {
const blobClient = blobServiceClient.getContainerClient(containerName).getBlobClient(blobName);
await blobClient.setTags(tags);
console.log(`Tags set for blob "${blobName}".`);
}
// Usage:
// setBlobTags("my-container", "my-text-blob.txt", { environment: "production", project: "api" });
Managing Containers
The BlobServiceClient provides methods to create, list, and delete containers within your storage account.
Example: Creating a Container
async function createContainer(containerName) {
const { containerClient } = await blobServiceClient.createContainer(containerName);
console.log(`Container "${containerName}" created.`);
return containerClient;
}
// Usage:
// createContainer("new-container");
Advanced Topics
Explore advanced features such as: