How to use Node.js with the local Azure Storage Emulator
As you may know, the Azure SDK for Node.js includes a Storage Emulator for development purposes. This lets you code against a local storage system using the Azure table, blob, and queue APIs rather than needing something configured in the public cloud. Need to develop on your laptop while midair on a flight? No problem.
You can switch between using the local Storage Emulator and the public cloud by using environment variables. Take a look at the open source SDK on GitHub, and you’ll see in serviceclient.js that several environment variables are parsed:
/*
* Used environment variables.
* @enum {string}
*/
ServiceClient.EnvironmentVariables = {
AZURE_STORAGE_ACCOUNT: 'AZURE_STORAGE_ACCOUNT',
AZURE_STORAGE_ACCESS_KEY: 'AZURE_STORAGE_ACCESS_KEY',
AZURE_SERVICEBUS_NAMESPACE: 'AZURE_SERVICEBUS_NAMESPACE',
AZURE_SERVICEBUS_ISSUER: 'AZURE_SERVICEBUS_ISSUER',
AZURE_SERVICEBUS_ACCESS_KEY: 'AZURE_SERVICEBUS_ACCESS_KEY',
AZURE_WRAP_NAMESPACE: 'AZURE_WRAP_NAMESPACE',
HTTP_PROXY: 'HTTP_PROXY',
HTTPS_PROXY: 'HTTPS_PROXY',
ALL_PROXY: 'ALL_PROXY',
EMULATED: 'EMULATED'
};
If an environment variable called EMULATED
is set, the Azure module will use the Storage Emulator without you having to explicitly pass credentials to the storage service. Here’s how you set it in PowerShell:
PS> $env:EMULATED = "true"
PS> ls env:EMULATED
Name Value
---- -----
EMULATED true
Incidentally, ls
and dir
are just aliases to Get-ChildItem
. You could also use Set-Variable
and Get-Variable
. Once set, you can create a handle to the storage service without passing in any credentials. Here’s how it looks for table storage:
var azure = require('azure');
var tableService = azure.createTableService();
If you like, you can still pass in the credentials for the Storage Emulator without setting an environment variable. The credentials are also defined in serviceclient.js. Aaron Stannard has a blog post which shows you how.
If you want your Node application to run on your local machine and talk to a live Azure service in the public cloud, keep an eye out for a blog post I’ll be making next.
You can automatically set environment variables by storing the commands in your PowerShell profile, which is simply a script which runs when PowerShell launches:
PS> $profile
C:\Users\Matt\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1
PS> test-path $profile
False
PS> mkdir $HOME\Documents\WindowsPowerShell
PS> notepad $profile
Make sure you’ve enabled the running of scripts with Set-ExecutionPolicy RemoteSigned
first. Run that as Administrator.
Node applications on any platform can use the Azure module to talk to the public cloud, but the Storage Emulator is only available on Windows.
Comments
Anonymous
November 26, 2014
I imagine this works great on windows. But what are all us developers not using windows to do?Anonymous
December 01, 2014
@Jmaxxxz: the emulator only runs on Windows. I don't believe other big cloud providers offer any emulators at all, so if you're not on Windows then your options are the same as with any other provider.