What is azure-storage?
The azure-storage npm package is a client library for working with Azure Storage services, including Blob, File, Queue, and Table storage. It allows developers to interact with these services programmatically, enabling tasks such as uploading and downloading files, managing queues, and working with table data.
What are azure-storage's main functionalities?
Blob Storage
Blob Storage allows you to store large amounts of unstructured data, such as text or binary data. The code sample demonstrates how to upload a text file to a blob container.
const azure = require('azure-storage');
const blobService = azure.createBlobService();
// Upload a text file to a container
blobService.createBlockBlobFromText('mycontainer', 'myblob', 'Hello, World!', function(error, result, response) {
if (!error) {
console.log('Blob uploaded successfully');
}
});
File Storage
File Storage provides a way to store and access files in the cloud. The code sample shows how to create a file share and upload a text file to it.
const azure = require('azure-storage');
const fileService = azure.createFileService();
// Create a share and upload a file
fileService.createShareIfNotExists('myshare', function(error, result, response) {
if (!error) {
fileService.createFileFromText('myshare', '', 'myfile', 'Hello, World!', function(error, result, response) {
if (!error) {
console.log('File uploaded successfully');
}
});
}
});
Queue Storage
Queue Storage provides reliable messaging for workflow processing and communication between different parts of your application. The code sample demonstrates how to create a queue and add a message to it.
const azure = require('azure-storage');
const queueService = azure.createQueueService();
// Create a queue and add a message
queueService.createQueueIfNotExists('myqueue', function(error, result, response) {
if (!error) {
queueService.createMessage('myqueue', 'Hello, World!', function(error, result, response) {
if (!error) {
console.log('Message added to queue');
}
});
}
});
Table Storage
Table Storage offers a NoSQL key-value store for rapid development using massive semi-structured datasets. The code sample shows how to create a table and insert an entity into it.
const azure = require('azure-storage');
const tableService = azure.createTableService();
// Create a table and insert an entity
const entGen = azure.TableUtilities.entityGenerator;
const task = {
PartitionKey: entGen.String('tasks'),
RowKey: entGen.String('1'),
description: entGen.String('Task 1'),
dueDate: entGen.DateTime(new Date(Date.UTC(2023, 10, 1)))
};
tableService.createTableIfNotExists('mytable', function(error, result, response) {
if (!error) {
tableService.insertEntity('mytable', task, function(error, result, response) {
if (!error) {
console.log('Entity inserted');
}
});
}
});
Other packages similar to azure-storage
aws-sdk
The aws-sdk package is the official AWS SDK for JavaScript, providing a comprehensive set of tools for interacting with AWS services, including S3 for object storage, DynamoDB for NoSQL databases, SQS for message queuing, and more. It is similar to azure-storage in that it provides a way to interact with cloud storage and other services, but it is specific to Amazon Web Services.
google-cloud
The google-cloud package is the official Google Cloud client library for Node.js. It provides tools for interacting with Google Cloud services, such as Google Cloud Storage for object storage, Firestore for NoSQL databases, and Pub/Sub for messaging. Like azure-storage, it offers a way to interact with cloud storage and other services, but it is specific to Google Cloud Platform.
ibm-cos-sdk
The ibm-cos-sdk package is the official SDK for IBM Cloud Object Storage. It provides tools for interacting with IBM's cloud storage services, allowing for operations such as uploading and downloading files, managing buckets, and more. It is similar to azure-storage in that it provides a way to interact with cloud storage, but it is specific to IBM Cloud.
Microsoft Azure Storage SDK for Node.js
This project provides a Node.js package that makes it easy to consume and manage Microsoft Azure Storage Services.
If you are looking for documentation for the Azure SDK for Node.js, see http://dl.windowsazure.com/nodedocs/index.html or visit https://github.com/Azure/azure-sdk-for-node. While the Azure SDK for Node.js provides support for working with Azure Storage, you should consider using the Azure Storage SDK as it supports features not available in the Azure SDK for Node.js
Features
- Tables
- Create/Delete Tables
- Query/Create/Read/Update/Delete Entities
- Blobs
- Create/Read/Update/Delete Blobs
- Queues
- Create/Delete Queues
- Insert/Peek Queue Messages
- Advanced Queue Operations
Getting Started
Install
npm install azure-storage
Usage
var azure = require('azure-storage');
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);
Table Storage
To ensure a table exists, call createTableIfNotExists:
var azure = require('azure-storage');
var tableService = azure.createTableService();
tableService.createTableIfNotExists('mytable', function(error, result, response){
if(!error){
}
});
A new entity can be added by calling insertEntity:
var azure = require('azure-storage');
var tableService = azure.createTableService(),
var entGen = azure.TableUtilities.entityGenerator;
var entity = { PartitionKey: entGen.String('part2'),
RowKey: entGen.String('row1'),
boolValueTrue: entGen.Boolean(true),
boolValueFalse: entGen.Boolean(false),
intValue: entGen.Int32(42),
dateValue: entGen.DateTime(new Date(Date.UTC(2011, 10, 25))),
complexDateValue: entGen.DateTime(new Date(Date.UTC(2013, 02, 16, 01, 46, 20)))
};
tableService.insertEntity('mytable',entity, function (error, result, response) {
if(!error){
}
});
Instead of creating entities manually, you can use entityGenerator:
var azure = require('azure-storage');
var entGen = azure.TableUtilities.entityGenerator;
var task = {
PartitionKey: entGen.String('hometasks'),
RowKey: entGen.String('1'),
description: entGen.String('take out the trash'),
dueDate: entGen.DateTime(new Date(Date.UTC(2015, 6, 20))),
};
The method retrieveEntity can then be used to fetch the entity that was just inserted:
var azure = require('azure-storage');
var tableService = azure.createTableService();
tableService.retrieveEntity('mytable', 'part2', 'row1', function(error, result, response){
if(!error){
}
});
Use TableQuery to build complex queries:
var azure = require('azure-storage');
var tableService = azure.createTableService();
var query = azure.TableQuery()
.top(5)
.where('PartitionKey eq ?', 'part2');
tableSvc.queryEntities('mytable', query, null, function(error, result, response) {
if(!error) {
}
});
Blob Storage
The createContainerIfNotExists method can be used to create a
container in which to store a blob:
var azure = require('azure-storage');
var blobService = azure.createBlobService();
blobService.createContainerIfNotExists('taskcontainer', {publicAccessLevel : 'blob'}, function(error, result, response){
if(!error){
}
});
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 createBlockBlobFromFile can be used.
var azure = require('azure-storage');
var blobService = azure.createBlobService();
blobService.createBlockBlobFromFile('mycontainer', 'taskblob', 'task1-upload.txt', function(error, result, response){
if(!error){
}
});
For page blobs, use createPageBlobFromFile. 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, getBlockBlobToStream downloads the blob to a stream:
var blobService = azure.createBlobService();
var fs = require('fs');
blobService.getBlockBlobToStream('mycontainer', 'taskblob', fs.createWriteStream('output.txt'), function(error, result, response){
if(!error) {
}
});
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.
var azure = require('azure-storage');
var blobService = azure.createBlobService();
var startDate = new Date();
var expiryDate = new Date(startDate);
expiryDate.setMinutes(startDate.getMinutes() + 100);
startDate.setMinutes(startDate.getMinutes() - 100);
var sharedAccessPolicy = {
AccessPolicy: {
Permissions: azure.BlobUtilities.SharedAccessPermissions.READ,
Start: startDate,
Expiry: expiryDate
},
};
var token = blobService.generateSharedAccessSignature(containerName, blobName, sharedAccessPolicy);
var sasUrl = blobService.getUrl(containerName, blobName, token);
Queue Storage
The createQueueIfNotExists method can be used to ensure a queue exists:
var azure = require('azure-storage');
var queueService = azure.createQueueService();
queueService.createQueueIfNotExists('taskqueue', function(error){
if(!error){
}
});
The createMessage method can then be called to insert the message into the queue:
var queueService = azure.createQueueService();
queueService.createMessage('taskqueue', 'Hello world!', function(error){
if(!error){
}
});
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.
var queueService = azure.createQueueService(),
queueName = 'taskqueue';
queueService.getMessages(queueName, function(error, serverMessages){
if(!error){
queueService.deleteMessage(queueName, serverMessages[0].messageid, serverMessages[0].popreceipt, function(error){
if(!error){
}
});
}
});
Code Samples
How-Tos focused around accomplishing specific tasks are available on the Microsoft Azure Node.js Developer Center.
Running Tests
In order to run the tests, the following environment variables need to be set up using an admin command prompt:
AZURE_STORAGE_CONNECTION_STRING="valid storage connection string"
or
AZURE_STORAGE_ACCOUNT="valid storage account name"
AZURE_STORAGE_ACCESS_KEY="valid storage account key"
In order to be able to use a proxy like fiddler, an additional environment variable should be set up:
HTTP_PROXY=http://127.0.0.1:8888
The tests can then be run from the module's root directory using:
npm test
Need Help?
Be sure to check out the Microsoft Azure Developer Forums on MSDN if you have trouble with the provided code or use StackOverflow.
Learn More
Contribute
We gladly accept community contributions.
- Issues: Please report bugs using the Issues section of GitHub
- Forums: Interact with the development teams on StackOverflow or the Microsoft Azure Forums
- Source Code Contributions: If you would like to become an active contributor to this project please follow the instructions provided in Microsoft Azure Projects Contribution Guidelines.
For general suggestions about Microsoft Azure please use our UserVoice forum.