This article shows you how to connect to Azure Blob Storage by using the Azure Blob Storage client library for JavaScript. Once connected, use the developer guides to learn how your code can operate on containers, blobs, and features of the Blob Storage service.
If you're looking to start with a complete example, see the client library quickstart for JavaScript or TypeScript.
API reference | Package (npm) | Library source code | Samples | Give feedback
PrerequisitesThis section walks you through preparing a project to work with the Azure Blob Storage client library for JavaScript.
Open a command prompt and navigate to your project folder. Change <project-directory>
to your folder name:
cd <project-directory>
If you don't have a package.json
file already in your directory, initialize the project to create the file:
npm init -y
From your project directory, install packages for the Azure Blob Storage and Azure Identity client libraries using the npm install
or yarn add
commands. The @azure/identity package is needed for passwordless connections to Azure services.
npm install @azure/storage-blob @azure/identity
npm install typescript @azure/storage-blob @azure/identity
To connect an app to Blob Storage, create an instance of the BlobServiceClient class. This object is your starting point to interact with data resources at the storage account level. You can use it to operate on the storage account and its containers. You can also use the service client to create container clients or blob clients, depending on the resource you need to work with.
To learn more about creating and managing client objects, including best practices, see Create and manage client objects that interact with data resources.
You can authorize a BlobServiceClient
object by using a Microsoft Entra authorization token, an account access key, or a shared access signature (SAS). For optimal security, Microsoft recommends using Microsoft Entra ID with managed identities to authorize requests against blob data. For more information, see Authorize access to blobs using Microsoft Entra ID.
To authorize with Microsoft Entra ID, you need to use a security principal. Which type of security principal you need depends on where your app runs. Use the following table as a guide:
Where the app runs Security principal Guidance Local machine (developing and testing) Service principal To learn how to register the app, set up a Microsoft Entra group, assign roles, and configure environment variables, see Authorize access using developer service principals Local machine (developing and testing) User identity To learn how to set up a Microsoft Entra group, assign roles, and sign in to Azure, see Authorize access using developer credentials Hosted in Azure Managed identity To learn how to enable managed identity and assign roles, see Authorize access from Azure-hosted apps using a managed identity Hosted outside of Azure (for example, on-premises apps) Service principal To learn how to register the app, assign roles, and configure environment variables, see Authorize access from on-premises apps using an application service principal Authorize access using DefaultAzureCredentialAn easy and secure way to authorize access and connect to Blob Storage is to obtain an OAuth token by creating a DefaultAzureCredential instance. You can then use that credential to create a BlobServiceClient
object.
The following example creates a BlobServiceClient
object using DefaultAzureCredential
:
const accountName = "<account-name>";
const accountURL = `https://${accountName}.blob.core.windows.net`;
const blobServiceClient = new BlobServiceClient(
accountURL,
new DefaultAzureCredential()
);
This code example can be used for JavaScript or TypeScript projects.
To use a shared access signature (SAS) token, append the token to the account URL string separated by a ?
delimiter. Then, create a BlobServiceClient
object with the URL.
const accountName = "<account-name>";
const sasToken = "<sas-token>";
const accountURL = `https://${accountName}.blob.core.windows.net?${sasToken}`;
const blobServiceClient = new BlobServiceClient(accountURL);
This code example can be used for JavaScript or TypeScript projects.
To learn more about generating and managing SAS tokens, see the following articles:
Note
For scenarios where shared access signatures (SAS) are used, Microsoft recommends using a user delegation SAS. A user delegation SAS is secured with Microsoft Entra credentials instead of the account key.
To use a storage account shared key, provide the key as a string and initialize a BlobServiceClient
object.
const credential = new StorageSharedKeyCredential(accountName, accountKey);
const blobServiceClient = new BlobServiceClient(
`https://${accountName}.blob.core.windows.net`,
credential
);
This code example can be used for JavaScript or TypeScript projects.
You can also create a BlobServiceClient
object using a connection string.
const blobServiceClient = BlobServiceClient.fromConnectionString(connectionString);
For information about how to obtain account keys and best practice guidelines for properly managing and safeguarding your keys, see Manage storage account access keys.
Important
The account access key should be used with caution. If your account access key is lost or accidentally placed in an insecure location, your service may become vulnerable. Anyone who has the access key is able to authorize requests against the storage account, and effectively has access to all the data. DefaultAzureCredential
provides enhanced security features and benefits and is the recommended approach for managing authorization to Azure services.
As you build apps to work with data resources in Azure Blob Storage, your code primarily interacts with three resource types: storage accounts, containers, and blobs. To learn more about these resource types, how they relate to one another, and how apps interact with resources, see Understand how apps interact with Blob Storage data resources.
The following guides show you how to access data and perform specific actions using the Azure Storage client library for JavaScript:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4