Partajați prin


Azure Blob Storage code samples using JavaScript version 11.x client libraries

This article shows code samples that use version 11.x of the Azure Blob Storage client library for JavaScript.

On March 31, 2023, we retired support for Azure SDK libraries which do not conform to the current Azure SDK guidelines. The new Azure SDK libraries are updated regularly to drive consistent experiences and strengthen your security posture. It's recommended that you transition to the new Azure SDK libraries to take advantage of the new capabilities and critical security updates.

Although the older libraries can still be used beyond March 31, 2023, they'll no longer receive official support and updates from Microsoft. For more information, see the support retirement announcement.

Build a highly available app with Blob Storage

Download the sample project and unzip the file. You can also use git to download a copy of the application to your development environment. The sample project contains a basic Node.js application.

git clone https://github.com/Azure-Samples/storage-node-v10-ha-ra-grs.git

Configure the sample

To run this sample, you must add your storage account credentials to the .env.example file and then rename it to .env.

AZURE_STORAGE_ACCOUNT_NAME=<replace with your storage account name>
AZURE_STORAGE_ACCOUNT_ACCESS_KEY=<replace with your storage account access key>

You can find this information in the Azure portal by navigating to your storage account and selecting Access keys in the Settings section.

Install the required dependencies by opening a command prompt, navigating to the sample folder, then entering npm install.

Run the console application

To run the sample, open a command prompt, navigate to the sample folder, then enter node index.js.

The sample creates a container in your Blob storage account, uploads HelloWorld.png into the container, then repeatedly checks whether the container and image have replicated to the secondary region. After replication, it prompts you to enter D or Q (followed by ENTER) to download or quit. Your output should look similar to the following example:

Created container successfully: newcontainer1550799840726
Uploaded blob: HelloWorld.png
Checking to see if container and blob have replicated to secondary region.
[0] Container has not replicated to secondary region yet: newcontainer1550799840726 : ContainerNotFound
[1] Container has not replicated to secondary region yet: newcontainer1550799840726 : ContainerNotFound
...
[31] Container has not replicated to secondary region yet: newcontainer1550799840726 : ContainerNotFound
[32] Container found, but blob has not replicated to secondary region yet.
...
[67] Container found, but blob has not replicated to secondary region yet.
[68] Blob has replicated to secondary region.
Ready for blob download. Enter (D) to download or (Q) to quit, followed by ENTER.
> D
Attempting to download blob...
Blob downloaded from primary endpoint.
> Q
Exiting...
Deleted container newcontainer1550799840726

Understand the code sample

With the Node.js V10 SDK, callback handlers are unnecessary. Instead, the sample creates a pipeline configured with retry options and a secondary endpoint. This configuration allows the application to automatically switch to the secondary pipeline if it fails to reach your data through the primary pipeline.

const accountName = process.env.AZURE_STORAGE_ACCOUNT_NAME;
const storageAccessKey = process.env.AZURE_STORAGE_ACCOUNT_ACCESS_KEY;
const sharedKeyCredential = new SharedKeyCredential(accountName, storageAccessKey);

const primaryAccountURL = `https://${accountName}.blob.core.windows.net`;
const secondaryAccountURL = `https://${accountName}-secondary.blob.core.windows.net`;

const pipeline = StorageURL.newPipeline(sharedKeyCredential, {
  retryOptions: {
    maxTries: 3,
    tryTimeoutInMs: 10000,
    retryDelayInMs: 500,
    maxRetryDelayInMs: 1000,
    secondaryHost: secondaryAccountURL
  }
});