AWS News Blog

AWS SDK for Node.js – Now Generally Available

The General Availability (GA) release of the AWS SDK for Node.js is now available and can be installed through npm as aws-sdk. We have added a number of features since the preview release including bound parameters, streams, IAM roles for EC2 instances, version locking, and proxies.

Here are a couple of examples to help you to understand bound parameters and streams.

Bound Parameters
You can now pass parameters into the constructor of any service object. These parameters will be intelligently merged with request parameters for each call. This makes it easy to create a service object that is bound to a specific resource. In the following example we create an Amazon S3 service object and bind it to a bucket and key.

obj = new AWS.S3({ params: { Bucket: 'bucket-name', Key: 'object-key' })    obj.headObject(function (err, data) {  	// no request params required  });    obj.putObject({ body: 'data' }, function (err, data) {  	// merges body param with Bucket and Key  });  

Streams
You can use the createReadStream() method on a request object to get a handle to a stream object which supports piping raw HTTP body data to a file. This is especially useful when streaming objects to streams like filesystem objects. The following example shows how you can stream an object from Amazon S3 directly to a file on disk:

var s3 = new AWS.S3();  var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};  var file = require('fs').createWriteStream('/path/to/file.jpg');  s3.getObject(params).createReadStream().pipe(file);  

Alternatively, you can register an ‘httpData’ event listener on the request object to access each chunk of data received across the wire (as Buffer objects):

var s3 = new AWS.S3();  var params = {Bucket: 'myBucket', Key: 'myImageFile.jpg'};  var file = require('fs').createWriteStream('/path/to/file.jpg');    s3.getObject(params).      on('httpData', function(chunk) { file.write(chunk); }).      on('httpDone', function() { file.end(); }).      send();  

For more examples, see the Getting Started Guide.

Resources

Here are some additional resources to get your started:

— Jeff;

TAGS:
Jeff Barr

Jeff Barr

Jeff Barr is Chief Evangelist for AWS. He started this blog in 2004 and has been writing posts just about non-stop ever since.