Created
September 10, 2012 12:10
-
-
Save richorama/3690582 to your computer and use it in GitHub Desktop.
Writing to a Windows Azure Storage Queue from Mobile Services
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var request = require('request'); | |
var crypto = require('crypto'); | |
var url = require('url'); | |
function insert(item, user, request) { | |
put_message(account, 'queuename', item.text); | |
request.execute(); | |
} | |
var account = { | |
name : "ACOUNT_NAME", | |
key : "ACCOUNT_KEY", | |
queue_storage_url : "https://ACOUNT_NAME.queue.core.windows.net" | |
} | |
var nl = '\n'; | |
var canonicalized_headers = function(req) { | |
var h = to_array(req.headers).sort(); | |
var buffer = ''; | |
for (var k in h) { | |
if (h[k][0].startsWith('x-ms-')) | |
buffer += h[k][0] + ":" + h[k][1] + nl; | |
} | |
return buffer | |
} | |
String.prototype.startsWith = function(str) { | |
return (this.indexOf(str) === 0); | |
} | |
var to_array = function(x) { | |
var a = []; | |
for (var k in x) | |
a.push([k, x[k]]); | |
return a; | |
} | |
var canonicalized_resource_format_1 = function(name, req) { | |
var x = url.parse(req.url,true); | |
var s = "/" + name + x.pathname; | |
// http://msdn.microsoft.com/en-us/library/windowsazure/dd179428.aspx | |
for (var k in x.query) { | |
// don't add dollar parameters (i.e. the table queries) | |
if (k.charAt(0) != "$") { | |
s += nl + k + ":" + x.query[k]; | |
} | |
} | |
return s; | |
} | |
var optional = function(x) { | |
return (x ? x : "") + nl; | |
} | |
var string_to_sign_1 = function(name, req) { | |
return "" + | |
req.method + nl + | |
optional(req.headers['Content-Encoding']) + | |
optional(req.headers['Content-Language']) + | |
optional(req.headers['Content-Length']) + | |
optional(req.headers['Content-MD5']) + | |
optional(req.headers['Content-Type']) + | |
optional(req.headers['Date']) + | |
optional(req.headers['If-Modified-Since']) + | |
optional(req.headers['If-Match']) + | |
optional(req.headers['If-None-Match']) + | |
optional(req.headers['If-Unmodified-Since']) + | |
optional(req.headers['Range']) + | |
canonicalized_headers(req) + | |
canonicalized_resource_format_1(name,req); | |
} | |
var pad = function(num, size) { | |
var s = "000000000" + num; | |
return s.substr(s.length-size); | |
} | |
function shared_key_for_blob_and_queue_services (account, req) { | |
req.headers = req.headers || {}; | |
req.headers['x-ms-date'] = (new Date()).toUTCString(); | |
req.headers['x-ms-version'] = '2009-09-19'; | |
var s = string_to_sign_1(account.name, req); | |
req.headers['Authorization'] = "SharedKey " + | |
account.name + ":" + | |
hmac_string(account.key,s ); | |
} | |
var hmac_string = function(key, str) { | |
var secret = (new Buffer(key, 'base64')).toString('binary'); | |
var hmac = crypto.createHmac('sha256', secret); | |
return hmac.update(str).digest('base64'); | |
} | |
var put_message = function(account, queue_name, message) { | |
var content = "<QueueMessage><MessageText><![CDATA[" + (new Buffer(message)).toString("base64") + "]]></MessageText></QueueMessage>"; | |
var req = { | |
url: account.queue_storage_url + '/' + queue_name + '/messages', | |
method: 'POST', | |
body: content, | |
headers:{ | |
"Content-Length": content.length | |
} | |
}; | |
shared_key_for_blob_and_queue_services(account,req); | |
request.post(req, function (error, response, body) { | |
if (error){ | |
console.error(error); | |
} | |
}); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment