Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
azure-storage
Advanced tools
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.
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');
}
});
}
});
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.
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.
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.
This project provides a Node.js package and a browser compatible JavaScript Client Library that makes it easy to consume and manage Microsoft Azure Storage Services.
This README page is a reference to the SDK v2. For the new SDK v10, go to Storage SDK v10 for JavaScript (Preview).
SDK Name | Version | Description | NPM/API Reference Links |
---|---|---|---|
Storage SDK v10 for JavaScript | v10-Preview | The next generation async Storage SDK (Blob only, async and promise support) | NPM - Reference |
Storage SDK v2 for JavaScript | v2 | Legacy Storage SDK in this repository (Blob/Queue/File/Table, callback style) | NPM - Reference |
Azure Management SDKs for JavaScript | v2 | Management SDKs including Storage Resource Provider APIs | NPM - Reference |
Please check details on API reference documents:
npm install azure-storage
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);
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) {
// if result = true, container was created.
// if result = false, container already existed.
}
});
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.
var azure = require('azure-storage');
var blobService = azure.createBlobService();
blobService.createBlockBlobFromLocalFile('mycontainer', 'taskblob', 'task1-upload.txt', function(error, result, response) {
if (!error) {
// file uploaded
}
});
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:
var blobService = azure.createBlobService();
var fs = require('fs');
blobService.getBlobToStream('mycontainer', 'taskblob', fs.createWriteStream('output.txt'), function(error, result, response) {
if (!error) {
// blob retrieved
}
});
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);
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) {
// result contains true if created; false if already exists
}
});
A new entity can be added by calling insertEntity or insertOrReplaceEntity:
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) {
// result contains the ETag for the new entity
}
});
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) {
// result contains the entity
}
});
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.
var azure = require('azure-storage');
var tableService = azure.createTableService();
var entity = {
PartitionKey: entGen.String('part2'),
RowKey: entGen.String('row1'),
taskDone: entGen.Boolean(true),
};
tableService.insertOrReplaceEntity('mytable', entity, function(error, result, response) {
if (!error) {
// result contains the entity with field 'taskDone' set to `true`
}
});
Use TableQuery to build complex queries:
var azure = require('azure-storage');
var tableService = azure.createTableService();
var query = new azure.TableQuery()
.top(5)
.where('PartitionKey eq ?', 'part2');
tableService.queryEntities('mytable', query, null, function(error, result, response) {
if (!error) {
// result.entries contains entities matching the query
}
});
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) {
// Queue exists
}
});
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) {
// Message inserted
}
});
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) {
// Process the message in less than 30 seconds, the message
// text is available in serverMessages[0].messageText
queueService.deleteMessage(queueName, serverMessages[0].messageId, serverMessages[0].popReceipt, function(error) {
if (!error) {
// Message deleted
}
});
}
});
The createShareIfNotExists method can be used to create a share in which to store a file or a directory of files:
var azure = require('azure-storage');
var fileService = azure.createFileService();
fileService.createShareIfNotExists('taskshare', function(error, result, response) {
if (!error) {
// if result = true, share was created.
// if result = false, share already existed.
}
});
To create a directory, the method createDirectoryIfNotExists can be used.
var azure = require('azure-storage');
var fileService = azure.createFileService();
fileService.createDirectoryIfNotExists('taskshare', 'taskdirectory', function(error, result, response) {
if (!error) {
// if result.created = true, share was created.
// if result.created = false, share already existed.
}
});
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.
var azure = require('azure-storage');
var fileService = azure.createFileService();
fileService.createFileFromLocalFile('taskshare', 'taskdirectory', 'taskfile', 'task1-upload.txt', function(error, result, response) {
if (!error) {
// file uploaded
}
});
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.
var stream = require('stream');
var azure = require('azure-storage');
var fileService = azure.createFileService();
var fileStream = new stream.Readable();
fileStream.push(myFileBuffer);
fileStream.push(null);
fileService.createFileFromStream('taskshare', 'taskdirectory', 'taskfile', fileStream, myFileBuffer.length, function(error, result, response) {
if (!error) {
// file uploaded
}
});
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.
var azure = require('azure-storage');
var fileService = azure.createFileService();
var text = 'Hello World!';
fileService.createFileFromText('taskshare', 'taskdirectory', 'taskfile', text, function(error, result, response) {
if (!error) {
// file created
}
});
There are also several ways to download files. For example, getFileToStream downloads the file to a stream:
var fileService = azure.createFileService();
var fs = require('fs');
fileService.getFileToStream('taskshare', 'taskdirectory', 'taskfile', fs.createWriteStream('output.txt'), function(error, result, response) {
if (!error) {
// file retrieved
}
});
The getServiceProperties method can be used to fetch the logging, metrics and CORS settings on your storage account:
var azure = require('azure-storage');
var blobService = azure.createBlobService();
blobService.getServiceProperties(function(error, result, response) {
if (!error) {
var serviceProperties = result;
// properties are fetched
}
});
The setServiceProperties method can be used to modify the logging, metrics and CORS settings on your storage account:
var azure = require('azure-storage');
var blobService = azure.createBlobService();
var serviceProperties = generateServiceProperties();
blobService.setServiceProperties(serviceProperties, function(error, result, response) {
if (!error) {
// properties are set
}
});
function generateServiceProperties() {
return serviceProperties = {
Logging: {
Version: '1.0',
Delete: true,
Read: true,
Write: true,
RetentionPolicy: {
Enabled: true,
Days: 10,
},
},
HourMetrics: {
Version: '1.0',
Enabled: true,
IncludeAPIs: true,
RetentionPolicy: {
Enabled: true,
Days: 10,
},
},
MinuteMetrics: {
Version: '1.0',
Enabled: true,
IncludeAPIs: true,
RetentionPolicy: {
Enabled: true,
Days: 10,
},
},
Cors: {
CorsRule: [
{
AllowedOrigins: ['www.azure.com', 'www.microsoft.com'],
AllowedMethods: ['GET', 'PUT'],
AllowedHeaders: ['x-ms-meta-data*', 'x-ms-meta-target*', 'x-ms-meta-xyz', 'x-ms-meta-foo'],
ExposedHeaders: ['x-ms-meta-data*', 'x-ms-meta-source*', 'x-ms-meta-abc', 'x-ms-meta-bcd'],
MaxAgeInSeconds: 500,
},
{
AllowedOrigins: ['www.msdn.com', 'www.asp.com'],
AllowedMethods: ['GET', 'PUT'],
AllowedHeaders: ['x-ms-meta-data*', 'x-ms-meta-target*', 'x-ms-meta-xyz', 'x-ms-meta-foo'],
ExposedHeaders: ['x-ms-meta-data*', 'x-ms-meta-source*', 'x-ms-meta-abc', 'x-ms-meta-bcd'],
MaxAgeInSeconds: 500,
},
],
},
};
}
When modifying the service properties, you can fetch the properties and then modify the them to prevent overwriting the existing settings.
var azure = require('azure-storage');
var blobService = azure.createBlobService();
blobService.getServiceProperties(function(error, result, response) {
if (!error) {
var serviceProperties = result;
// modify the properties
blobService.setServiceProperties(serviceProperties, function(error, result, response) {
if (!error) {
// properties are set
}
});
}
});
By default, no retry will be performed with service instances newly created by Azure storage client library for Node.js. Two pre-written retry polices ExponentialRetryPolicyFilter and LinearRetryPolicyFilter are available with modifiable settings, and can be used through associating filter. Any custom retry logic may be used by customizing RetryPolicyFilter instance.
For how to use pre-written retry policies and how to define customized retry policy, please refer to retrypolicysample in samples directory.
How-Tos focused around accomplishing specific tasks are available on the Microsoft Azure Node.js Developer Center.
Unit tests can then be run from the module's root directory using:
npm test
Running test is also supported by Grunt by:
grunt # mochaTest as the default task
By default the unit tests are ran with Nock recording data. To run tests against real storage account, please set environment variable to turn off Nock by:
set NOCK_OFF=true
and set up the following environment variable for storage account credentials by
set AZURE_STORAGE_CONNECTION_STRING="valid storage connection string"
To record the data in a test pass against real storage account for future Nock usage:
set AZURE_NOCK_RECORD=true
In order to be able to use a proxy like fiddler, an additional environment variable should be set up:
set NODE_TLS_REJECT_UNAUTHORIZED=0
set HTTP_PROXY=http://127.0.0.1:8888
On Linux, please use export
other than set
to set the variables.
Azure Storage Node.js Client Library is compatible with Browserify. This means you can bundle your Node.js application which depends on the Node.js Client Library using Browserify.
You can also choose to download the JavaScript Client Library provided by us, or generate the library by yourself. Please refer to the README.md under browser
folder for detailed usage guidelines.
It's recommended to use the Azure Storage JavaScript Client Library provided by us. Please download the latest library.
We also provide browserify bundle scripts which generate Azure Storage JavaScript Client Library. The bundle script reduces the size of the Storage Client Library by splitting into smaller files, one per storage service. For more detailed information, refer to README.md under browser
folder.
JsDoc can be generated by grunt jsdoc
.
To load the docs by devserver after generation, run grunt doc
and then browse the docs at http://localhost:8888.
Be sure to check out the Microsoft Azure Developer Forums on MSDN if you have trouble with the provided code or use StackOverflow.
We gladly accept community contributions.
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.
For general suggestions about Microsoft Azure please use our UserVoice forum.
FAQs
Microsoft Azure Storage Client Library for Node.js
We found that azure-storage demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 2 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.