Skip to content
background-image background-image

Using Azure Storage Blob

This example shows how to use @azure/storage-blob module.

Some authorization methods require types provided by @azure/identity module which is imported as azure.identity

Authorization using DefaultAzureCredential class is not supported, you can use ClientSecretCredential

@azure/storage-blob module is imported as azure.storageBlob

Authorize using ClientSecretCredential

const { ClientSecretCredential } = azure.identity;
const { BlobServiceClient } = azure.storageBlob;

const account = "<account>";
const clientSecretCredential = new ClientSecretCredential(
  "<tentant-id>",
  "<client-id>",
  "<client-secret>"
);

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  clientSecretCredential
);

Authorize using connection string

const { BlobServiceClient } = azure.storageBlob;

const connStr = "<connection string>";
const blobServiceClient = BlobServiceClient.fromConnectionString(connStr);

Authorize using StorageSharedKeyCredential

const { BlobServiceClient, StorageSharedKeyCredential } = azure.storageBlob;

const account = "<account>";
const accountKey = "<account key>";

// Use StorageSharedKeyCredential with storage account and account key
// StorageSharedKeyCredential is only available in Node.js runtime, not in browsers
const sharedKeyCredential = new StorageSharedKeyCredential(account, accountKey);
const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential
);

Authorize using SAS Token

const { BlobServiceClient } = azure.storageBlob;

const account = "<account>";
const sas = "<sas token>";
//sas token expiration is determined in azure
const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net${sas}`
);

List blob containers

let i = 1;
// blobServiceClient can be obtained using above examples
let containers = blobServiceClient.listContainers();
for await (const container of containers) {
  log.info(`Container #${i++}: ${container.name}`);
}

See Azure Storage Blob client library for full examples.