12.17.0 • Published 6 days ago

@azure/storage-blob v12.17.0

Weekly downloads
258,830
License
MIT
Repository
github
Last release
6 days ago

Azure Storage client library for JavaScript - Blob

Azure Blob storage is Microsoft's object storage solution for the cloud. Blob storage is optimized for storing massive amounts of unstructured data. Unstructured data is data that does not adhere to a particular data model or definition, such as text or binary data.

This project provides a client library in JavaScript that makes it easy to consume Microsoft Azure Blob Storage service.

Source code | Package (npm) | API Reference Documentation | Product documentation | Samples | Azure Storage Blob REST APIs

Key concepts

Features

  • Blob Storage
    • Get/Set Blob Service Properties
    • Create/List/Delete Containers
    • Create/Read/List/Update/Delete Block Blobs
    • Create/Read/List/Update/Delete Page Blobs
    • Create/Read/List/Update/Delete Append Blobs
  • Features new
    • Asynchronous I/O for all operations using the async methods
    • HttpPipeline which enables a high degree of per-request configurability
    • 1-to-1 correlation with the Storage REST API for clarity and simplicity

Compatibility

This library is compatible with Node.js and browsers, and validated against LTS Node.js versions (>=8.16.0) and latest versions of Chrome, Firefox and Edge.

Compatible with IE11

You need polyfills to make this library work with IE11. The easiest way is to use @babel/polyfill, or polyfill service.

You can also load separate polyfills for missed ES feature(s). This library depends on following ES features which need external polyfills loaded.

  • Promise
  • String.prototype.startsWith
  • String.prototype.endsWith
  • String.prototype.repeat
  • String.prototype.includes
  • Array.prototype.includes
  • Object.keys (Override IE11's Object.keys with ES6 polyfill forcely to enable ES6 behavior)
  • Symbol

Differences between Node.js and browsers

There are differences between Node.js and browsers runtime. When getting started with this library, pay attention to APIs or classes marked with "ONLY AVAILABLE IN NODE.JS RUNTIME" or "ONLY AVAILABLE IN BROWSERS".

Following features, interfaces, classes or functions are only available in Node.js
  • Shared Key Authorization based on account name and account key
    • SharedKeyCredential
  • Shared Access Signature(SAS) generation
    • generateAccountSASQueryParameters()
    • generateBlobSASQueryParameters()
  • Parallel uploading and downloading
    • BlockBlobClient.uploadFile()
    • BlockBlobClient.uploadStream()
    • BlobClient.downloadToBuffer()
    • BlobClient.downloadToFile()
Following features, interfaces, classes or functions are only available in browsers
  • Parallel uploading and downloading
    • BlockBlobClient.uploadBrowserData()

Getting started

NPM

The preferred way to install the Azure Blob Storage client library for JavaScript is to use the npm package manager. Simply type the following into a terminal window:

npm install @azure/storage-blob@12.0.0-preview.1

In your TypeScript or JavaScript file, import via following:

import * as Azure from "@azure/storage-blob";

Or

const Azure = require("@azure/storage-blob");

JavaScript bundle

To use the library with JS bundle in the browsers, simply add a script tag to your HTML pages pointing to the downloaded JS bundle file(s):

<script src="https://mydomain/azure-storage-blob.min.js"></script>

The JS bundled file is compatible with UMD standard, if no module system found, following global variable(s) will be exported:

  • azblob

Download

Download latest released JS bundles from links in the GitHub release page. Or from following links directly:

CORS

You need to set up Cross-Origin Resource Sharing (CORS) rules for your storage account if you need to develop for browsers. Go to Azure portal and Azure Storage Explorer, find your storage account, create new CORS rules for blob/queue/file/table service(s).

For example, you can create following CORS settings for debugging. But please customize the settings carefully according to your requirements in production environment.

  • Allowed origins: *
  • Allowed verbs: DELETE,GET,HEAD,MERGE,POST,OPTIONS,PUT
  • Allowed headers: *
  • Exposed headers: *
  • Maximum age (seconds): 86400

Examples

Create the blob service client

Use the constructor to create a instance of BlobServiceClient.

// Enter your storage account name and shared key
const account = "account";
const accountKey = "accountkey";

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

Create a new container

Use BlobServiceClient.getContainerClient() to create a new container.

// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerClient = blobServiceClient.getContainerClient(containerName);

const createContainerResponse = await containerClient.create();
console.log(`Create container ${containerName} successfully`, createContainerResponse.requestId);

List the containers

Use BlobServiceClient.listContainers() function to iterate the containers, with the new for-await-of syntax:

  let i = 1;
  let iter = await blobServiceClient.listContainers();
  for await (const container of iter) {
    console.log(`Container ${i++}: ${container.name}`);
  }

Alternatively without using for-await-of:

i = 1;
iter = blobServiceClient.listContainers();
let containerItem = await iter.next();
while (!containerItem.done) {
  console.log(`Container ${i++}: ${containerItem.value.name}`);
  containerItem = await iter.next();
}

In addition, pagination is supported for listing too via byPage():

  i = 1;
  for await (const response of blobServiceClient.listContainers().byPage({ maxPageSize: 20 })) {
    if (response.containerItems) {
      for (const container of response.containerItems) {
        console.log(`Container ${i++}: ${container.name}`);
      }
    }
  }

For a complete sample on iterating containers please see samples/iterators-containers.ts.

Create a blob by uploading data to

const content = "hello";
const blobName = "newblob" + new Date().getTime();
const blobClient = containerClient.getBlobClient(blobName);
const blockBlobClient = blobClient.getBlockBlobClient();
const uploadBlobResponse = await blockBlobClient.upload(content, content.length);
console.log(`Upload block blob ${blobName} successfully`, uploadBlobResponse.requestId);

List blobs inside a container

Similar to listing containers.

  let i = 1;
  let iter = await containerClient.listBlobsFlat();
  for await (const blob of iter) {
    console.log(`Blob ${i++}: ${blob.name}`);
  }

For a complete sample on iterating blobs please see samples/iterators-blobs.ts.

Download a blob and convert it to a string (Node.js)

// Get blob content from position 0 to the end
// In Node.js, get downloaded data by accessing downloadBlockBlobResponse.readableStreamBody
const downloadBlockBlobResponse = await blobClient.download(0);
console.log(
  "Downloaded blob content",
  await streamToString(downloadBlockBlobResponse.readableStreamBody)
);

// [Node.js only] A helper method used to read a Node.js readable stream into string
async function streamToString(readableStream) {
  return new Promise((resolve, reject) => {
    const chunks = [];
    readableStream.on("data", (data) => {
      chunks.push(data.toString());
    });
    readableStream.on("end", () => {
      resolve(chunks.join(""));
    });
    readableStream.on("error", reject);
  });
}

Download a blob and convert it to a string (Browsers)

  // Get blob content from position 0 to the end
  // In browsers, get downloaded data by accessing downloadBlockBlobResponse.blobBody
  const downloadBlockBlobResponse = await blobClient.download(0);
  console.log(
    "Downloaded blob content",
    await blobToString(downloadBlockBlobResponse.blobBody)
  );

// [Browsers only] A helper method used to convert a browser Blob into string.
export async function blobToString(blob: Blob): Promise<string> {
  const fileReader = new FileReader();
  return new Promise<string>((resolve, reject) => {
    fileReader.onloadend = (ev: any) => {
      resolve(ev.target!.result);
    };
    fileReader.onerror = reject;
    fileReader.readAsText(blob);
  });
}

A complete example of basic scenarios is at samples/basic.ts.

Troubleshooting

It could help diagnozing issues by turning on the console logging. Here's an example logger implementation. First, add a custom logger:

class ConsoleHttpPipelineLogger {
  constructor(minimumLogLevel) {
    this.minimumLogLevel = minimumLogLevel;
  }
  log(logLevel, message) {
    const logMessage = `${new Date().toISOString()} ${HttpPipelineLogLevel[logLevel]}: ${message}`;
    switch (logLevel) {
      case HttpPipelineLogLevel.ERROR:
        console.error(logMessage);
        break;
      case HttpPipelineLogLevel.WARNING:
        console.warn(logMessage);
        break;
      case HttpPipelineLogLevel.INFO:
        console.log(logMessage);
        break;
    }
  }
}

When creating the BlobServiceClient instance, pass the logger in the options

const blobServiceClient = new BlobServiceClient(
  `https://${account}.blob.core.windows.net`,
  sharedKeyCredential,
  {
    logger: new ConsoleHttpPipelineLogger(HttpPipelineLogLevel.INFO)
  }
);

Authenticating with Azure Active Directory

If you have registered an application with an Azure Active Directory tenant, you can assign it to an RBAC role in your Azure Storage account. This enables you to use the Azure.Identity library to authenticate with Azure Storage as shown in the azureAdAuth.ts sample.

Next steps

More code examples

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit https://cla.microsoft.com.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

@argoncs/libraries@microsoft/teamsfx-core@thingspro-cloud/sdkcreateme-nestjs-azure-storageskolid-chromeos-appopenpai-js-sdk@netinsight/sye-toolssnitch2requesthorha-file-serverctk-react@airvzxf/cachenpm-deploy-sample-uhjin@eztool/import-map-deployerdorsavi.storage.javascriptomms-pe-express-configcoding-house-components@leni-msft/oavdorsavi-storagevscode-azureappservice-laleaf-jobschina-dashboard@the-bds-maneger/clouds_uploads@haddad_zineddine/azure-blob-storage@project-carbon/sharedvh_component_test_copypc-versionhistory-spc-rulesengine-sjpc-rulesengine-sppc-notifications-spc-notifications-sh@cocs/commoncreate-ldsk-ui-boilerplate@paiondata/nextwiki@unblock-dev/drive-azurefsm-toolsintraactive-sdk-ui@everything-registry/sub-chunk-107elice-file-client@kurago-dev/node-red-contrib-azuretransfer-mp4-to-hlslivebundle-storage-azurenexxomnia-api-nodejsniosz-blobnjamnjam-climigrate-azure-storagemulter-az-storage-blobmkm-module-foldersmkm-module-trainingrecordsnativevideo-sdk-jsnest-microservices-storagenestjs-azure-storagenest-azure-storagenewlibrary-indusfusionlvlup-servermaikuromicrosoft-intunemany-cloudnodets-ms-coren8n-nodes-azure-blob-storagemedusa-plugin-file-azuremed-logmedusa-file-azuremedusa-file-azure-blobmedusa-file-azure-storage@things-factory/attachment-base@tphan32/data-transfer@vanducvo/cache@vatsdev/ui-utils@theroks/remotecache-azure@undp-data/geohub-cli@tomei/log@vaem-util/filesystem@tsart/alchemy-core@trulive/sye-tools@trustingsocial/billingquery@useblacksmith/cache@tomasjs/cli@thinkpixellab/snowflake-connector-nodejs@paperbits/azure@tweedegolf/sab-adapter-azure-blob@walmartlabs/cookie-cutter-azureadonis-drive-azure-storageadtracker-core@zoovu/runner-web-design-base@zhenglaizhang/oav@zhenglaizhang/publish-pipeline-result@wbce-d9/storage-driver-azureabs-sinqia-integration.consumer@winglang/sdk@vtfk/azure-blob-client@vtfk/azure-storage-blob@ts-common/azure-js-dev-tools@yukonsky/reg-publish-azure-blob-storage-plugin@actions/artifact@actions/cache@apoyo/files-azure@armhil/azure-blob-static-content-uploader@artisreit/cdn_deploy@almaviacx/gge-twilio-cli-plugins
12.17.0

5 months ago

12.23.0-beta.1

5 months ago

12.16.0-beta.1

8 months ago

12.17.0-beta.1

5 months ago

12.15.0

9 months ago

12.16.0

7 months ago

12.15.0-beta.1

10 months ago

12.14.0

12 months ago

12.13.0

1 year ago

12.13.0-beta.1

1 year ago

12.14.0-beta.1

12 months ago

12.12.0

1 year ago

12.12.0-beta.1

2 years ago

12.11.0

2 years ago

12.11.0-beta.1

2 years ago

12.10.0

2 years ago

12.10.0-beta.1

2 years ago

12.9.0

2 years ago

12.9.0-beta.4

2 years ago

12.9.0-beta.3

2 years ago

12.9.0-beta.1

2 years ago

12.9.0-beta.2

2 years ago

12.8.0

3 years ago

12.7.0

3 years ago

12.8.0-beta.1

3 years ago

12.6.0

3 years ago

12.6.0-beta.1

3 years ago

12.5.0

3 years ago

12.5.0-beta.1

3 years ago

12.4.1

3 years ago

12.4.0

3 years ago

12.4.0-beta.1

3 years ago

12.3.0

3 years ago

12.3.0-beta.1

3 years ago

12.2.1

4 years ago

12.2.0

4 years ago

12.2.0-preview.1

4 years ago

12.1.2

4 years ago

12.1.1

4 years ago

12.1.0

4 years ago

12.0.2

4 years ago

12.0.1

4 years ago

12.0.0

4 years ago

12.0.0-preview.5

4 years ago

12.0.0-preview.4

4 years ago

12.0.0-preview.3

5 years ago

10.5.0

5 years ago

10.4.1

5 years ago

12.0.0-preview.2

5 years ago

10.4.0

5 years ago

12.0.0-preview.1

5 years ago

10.3.0

5 years ago

10.2.0-preview

5 years ago

10.1.0-preview

5 years ago

10.0.0-preview

6 years ago