Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Snippet in node.js to download a file from an Azure Files SMB Share using the REST API
'use strict';
// Uses request: npm install --save request
const request = require('request');
const fs = require('fs');
const crypto = require('crypto');
const dummyLog = function (s) {
const debug = dummyLog, info = dummyLog, error = dummyLog;
const x_ms_version = '2018-03-28';
function downloadFromStorage(storageName, storageKey, shareName, filePath, outFilePath, callback) {
debug(`downloadFromStorage(${shareName}, ${filePath})`);
const mediaPath = `${shareName}${filePath}`;
const mediaUri = `https://${storageName}${mediaPath}`;
const x_ms_date = (new Date()).toUTCString();
const canonicalizedResource = `/${storageName}/${mediaPath}`;
const headers = {
'x-ms-date': x_ms_date,
'x-ms-version': x_ms_version
const canonicalizedHeaders = makeCanonicalizedHeaders(headers);
// SharedKeyLite
const stringToSign = `GET\n\n\n\n${canonicalizedHeaders}${canonicalizedResource}`;
const secret = Buffer.from(storageKey, 'base64');
const hmac256 = crypto.createHmac('sha256', secret);
hmac256.update(stringToSign, 'utf-8');
const sig = hmac256.digest('base64');
headers.Authorization = `SharedKeyLite ${storageName}:${sig}`;
uri: mediaUri,
headers: headers
.on('response', function (res) {
debug(`GET ${mediaPath} responding with ${res.statusCode}, Content-Type ${res.headers['content-type']}`);
if (res.statusCode === 200) {
.on('finish', function () {
info(`Downloaded ${mediaPath} on the fly.`);
return callback(null);
} else {
return callback(makeError(res.statusCode, `Could not download ${mediaPath} (status code ${res.statusCode})`));
.on('error', function (err) {
return callback(makeError(500, `An error occurred while downloading ${mediaPath}`));
function makeCanonicalizedHeaders(headers) {
let first = true;
let c = '';
for (let h in headers) {
if (!h.startsWith('x-ms-'))
c += `${h}:${headers[h]}\n`;
return c;
function makeError(status, message, innerError) {
const err = new Error(message);
err.status = status;
if (innerError)
err.innerError = innerError;
return err;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.