It also provides defaults for a number of core AWS headers and
request parameters, making it a very easy to query AWS services, or
build out a fully-featured AWS library.
Example
var http = require('http') , https = require('https') , aws4 = require('aws4')// given an options object you could pass to http.requestvar opts = { host: 'sqs.us-east-1.amazonaws.com', path: '/?Action=ListQueues' }aws4.sign(opts) // assumes AWS credentials are available in process.envconsole.log(opts)/*{ host: 'sqs.us-east-1.amazonaws.com', path: '/?Action=ListQueues', headers: { Host: 'sqs.us-east-1.amazonaws.com', 'X-Amz-Date': '20121226T061030Z', Authorization: 'AWS4-HMAC-SHA256 Credential=ABCDEF/20121226/us-east-1/sqs/aws4_request, ...' }
More options
// you can pass AWS credentials in explicitlyaws4.sign(opts, { accessKeyId: '', secretAccessKey: '' })// aws4 can infer the host from a service and regionopts = aws4.sign({ service: 'sqs', region: 'us-east-1', path: '/?Action=ListQueues' })// create a utility function to pipe to stdout (with https this time)function request(o) { https.request(o, function(res) { res.pipe(process.stdout) }).end(o.body || '') }// aws4 can infer the HTTP method if a body is passed in// method will be POST and Content-Type: 'application/x-www-form-urlencoded; charset=utf-8'request(aws4.sign({ service: 'iam', body: 'Action=ListGroups&Version=2010-05-08' }))/*<ListGroupsResponse xmlns="https://iam.amazonaws.com/doc/2010-05-08/">
API
aws4.sign(requestOptions, [credentials])
This calculates and populates the Authorization header of
requestOptions, and any other necessary AWS headers and/or request
options. Returns requestOptions as a convenience for chaining.
requestOptions is an object holding the same options that the node.js
http.request
function takes.
The following properties of requestOptions are used in the signing or
populated if they don't already exist:
hostname or host (will be determined from service and region if not given)
method (will use 'GET' if not given or 'POST' if there is a body)
path (will use '/' if not given)
body (will use '' if not given)
service (will be calculated from hostname or host if not given)
region (will be calculated from hostname or host or use 'us-east-1' if not given)
headers['Host'] (will use hostname or host or be calculated if not given)
headers['Content-Type'] (will use 'application/x-www-form-urlencoded; charset=utf-8'
if not given and there is a body)
headers['Date'] (used to calculate the signature date if given, otherwise new Date is used)
Your AWS credentials (which can be found in your
AWS console)
can be specified in one of two ways: