Giter Club home page Giter Club logo

azure-storage-js's Introduction

Azure Storage SDK V10 for JavaScript

  • @azure/storage-blob npm version

Introduction

This project provides a SDK in JavaScript that makes it easy to consume Microsoft Azure Storage services.

Please note that this version of the SDK is a compete overhaul of the current Azure Storage SDK for Node.js and JavaScript in Browsers, and is based on the new Storage SDK architecture.

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 SDK is compatible with Node.js and browsers, and validated against LTS Node.js versions (>=6.5) and latest versions of Chrome, Firefox and Edge.

Differences between Node.js and browsers

There are differences between Node.js and browsers runtime. When getting start with this SDK, 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
    • uploadFileToBlockBlob()
    • uploadStreamToBlockBlob()
    • downloadBlobToBuffer()
Following features, interfaces, classes or functions are only available in browsers
  • Parallel uploading and downloading
    • uploadBrowserDataToBlockBlob()

Compatible with IE11

You need polyfills to make this library work with IE11. The easiest way is to using @babel/polyfill. Or you can load separate polyfills for missed ES feature(s). This library depends on following ES6+ feature(s) which needs external polyfills loaded.

  • Promise

Getting Started

NPM

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

npm install @azure/storage-blob

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 SDK 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:

SDK Architecture

The Azure Storage SDK for JavaScript provides low-level and high-level APIs. Take Blob SDK as example:

  • ServiceURL, ContainerURL and BlobURL objects provide the low-level API functionality and map one-to-one to the Azure Storage Blob REST APIs.

  • The high-level APIs provide convenience abstractions such as uploading a large stream to a block blob (using multiple PutBlock requests).

Code Samples

const pipeline = StorageURL.newPipeline(
  new SharedKeyCredential("account", "accountkey"),
);

// List containers
const serviceURL = new ServiceURL(
  "https://account.blob.core.windows.net",
  pipeline,
);

let marker;
do {
  const listContainersResponse = await serviceURL.listContainersSegment(
    Aborter.None,
    marker,
  );

  marker = listContainersResponse.marker;
  for (const container of listContainersResponse.containerItems) {
    console.log(`Container: ${container.name}`);
  }
} while (marker);

// Create a container
const containerName = `newcontainer${new Date().getTime()}`;
const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName);

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

// Create a blob
const content = "hello";
const blobName = "newblob" + new Date().getTime();
const blobURL = BlobURL.fromContainerURL(containerURL, blobName);
const blockBlobURL = BlockBlobURL.fromBlobURL(blobURL);
const uploadBlobResponse = await blockBlobURL.upload(
  Aborter.None,
  content,
  content.length,
);
console.log(`Upload block blob ${blobName} successfully`);

// Get blob content in Node.js runtime
const downloadBlockBlobResponse = await blobURL.download(Aborter.None, 0);
console.log(
  "Downloaded blob content",
  downloadBlockBlobResponse.readableStreamBody.read(content.length).toString(),
);
console.log(`[headers]:${downloadBlockBlobResponse.headers}`);

// Delete container
await containerURL.delete(Aborter.None);
console.log("deleted container");

More Samples

License

This project is licensed under MIT.

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 [email protected] with any additional questions or comments.

azure-storage-js's People

Contributors

microsoftopensource avatar msftgits avatar xiaoningliu avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.