Node.js Microsoft Azure Storage SDK Tutorial to Upload & Download Files in Browser Using Javascript Full Project For Beginners

 

 

Getting Started

Install

Usage

When using the Storage SDK, you must provide connection information for the storage account to use. This can be provided using:

  • Environment variables – AZURE_STORAGE_ACCOUNT and AZURE_STORAGE_ACCESS_KEY, or AZURE_STORAGE_CONNECTION_STRING.
  • Constructors – For example, var tableSvc = azure.createTableService(accountName, accountKey);

Blob Storage

The createContainerIfNotExists method can be used to create a container in which to store a blob:

To upload a file (assuming it is called task1-upload.txt and it is placed in the same folder as the script below), the method createBlockBlobFromLocalFile can be used.

For page blobs, use createPageBlobFromLocalFile. There are other methods for uploading blobs also, such as createBlockBlobFromText or createPageBlobFromStream.

There are also several ways to download block and page blobs. For example, getBlobToStream downloads the blob to a stream:

To create a Shared Access Signature (SAS), use the generateSharedAccessSignature method. Additionally you can use the date helper functions to easily create a SAS that expires at some point relative to the current time.

Table Storage

To ensure a table exists, call createTableIfNotExists:

A new entity can be added by calling insertEntity or insertOrReplaceEntity:

Instead of creating entities manually, you can use entityGenerator:

The method retrieveEntity can then be used to fetch the entity that was just inserted:

The method replaceEntity or insertOrReplaceEntity can be called to update/edit an existing entry. In the following example we assume that an entity 'part2', 'row1' with a field 'taskDone' set to false already exists.

Use TableQuery to build complex queries:

Queue Storage

The createQueueIfNotExists method can be used to ensure a queue exists:

The createMessage method can then be called to insert the message into the queue:

It is then possible to call the getMessage method, process the message and then call deleteMessage inside the callback. This two-step process ensures messages don’t get lost when they are removed from the queue.

File Storage

The createShareIfNotExists method can be used to create a share in which to store a file or a directory of files:

To create a directory, the method createDirectoryIfNotExists can be used.

To upload a file (assuming it is called task1-upload.txt and it is placed in the same folder as the script below), the method createFileFromLocalFile can be used.

To upload a file from a stream, the method createFileFromStream can be used. The var myFileBuffer in the script below is a native Node Buffer, or ArrayBuffer object if within a browser environment.

To create a file from a text string, the method createFileFromText can be used. A Node Buffer or ArrayBuffer object containing the text can also be supplied.

There are also several ways to download files. For example, getFileToStream downloads the file to a stream:

Service Properties

The getServiceProperties method can be used to fetch the logging, metrics and CORS settings on your storage account:

The setServiceProperties method can be used to modify the logging, metrics and CORS settings on your storage account:

Leave a Reply