Javascript Azure Blob Storage Tips and Ticks 1

2022-12-30 Azure

Table of Contents

This tutorial will explain how to use Python to list blob files, upload blob files, copy blob files, check if blob file exists, delete blob files.

Azure

Preparing

Prepare Azure account

Create a new Storage Account

Get the connection string

Copy storage account name and access key.

Azure

Connect to Azure Blob

To connect to an Azure Blob Storage account from JavaScript, you can use the Azure Storage SDK for JavaScript.

Here’s an example of how you can use the azure-storage package to connect to an Azure Blob Storage account and list the blobs in a container:

// Azure Storage dependency
const {
    StorageSharedKeyCredential,
    BlobServiceClient,
  } = require("@azure/storage-blob");

async function main() {
  // Enter your storage account name and shared key
  const account = "<your storage account name>";
  const accountKey = "<your account key>";

  // Use the BlobServiceClient constructor to create a client
  // that references your storage account
  const blobServiceClient = new BlobServiceClient(
    `https://${account}.blob.core.windows.net`,
    new StorageSharedKeyCredential(account, accountKey)
  );

  // Use the listContainers method to list the containers in your storage account
  const containers = await blobServiceClient.listContainers();

  // Iterate over the containers and print their names
  for await (const container of containers) {
    console.log(`Container: ${container.name}`);
  }
}

main().catch((error) => {
  console.error(error);
});

This example uses the BlobServiceClient class from the @azure/storage-blob package to connect to the storage account and the listContainers method to list the containers in the account.

You can find more information about using the Azure Storage SDK for JavaScript in the documentation:

Azure Storage SDK for JavaScript

@azure/storage-blob package

List blob files

To list the blobs in a container in an Azure Blob Storage account using JavaScript, you can use the listBlobsFlat or listBlobsByHierarcy method of the ContainerClient class from the @azure/storage-blob package.

Here’s an example of how you can use the listBlobs method to list the blobs in a container:

// Azure Storage dependency
const {
    StorageSharedKeyCredential,
    BlobServiceClient,
  } = require("@azure/storage-blob");

async function main() {
  // Enter your storage account name and shared key
  const account = "xxxxxxxxxxxxxxx";
  const accountKey = "xxxxxxxxxxxxxxxxxxxxxxxxxx";

  // Use the BlobServiceClient constructor to create a client
  // that references your storage account
  const blobServiceClient = new BlobServiceClient(
    `https://${account}.blob.core.windows.net`,
    new StorageSharedKeyCredential(account, accountKey)
  );

  // Use the getContainerClient method to get a client for the container
  const containerClient = blobServiceClient.getContainerClient("test");

  // Use the listBlobs method to list the blobs in the container
  const blobs = await containerClient.listBlobsFlat();

  // Iterate over the blobs and print their names
  for await (const blob of blobs) {
    console.log(`Blob: ${blob.name}`);
  }
}

main().catch((error) => {
  console.error(error);
});

This example uses the BlobServiceClient class to connect to the storage account, the getContainerClient method to get a client for the container, and the listBlobsFlat method to list the blobs in the container.

You can find more information about using the listBlobs method and the ContainerClient class in the documentation:

listBlobs method: https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/containerclient?view=azure-node-latest#listblobs

ContainerClient class: https://docs.microsoft.com/en-us/javascript/api/@azure/storage-blob/containerclient?view=azure-node-latest

Subscribe and be the FIRST reader of our latest articles

* indicates required

Contact us