0

Can't seem to find where to specify the location of the object when I call getObject.

Basically, trying to get an object from s3 and download it. I am able to get the object with no issues but it does not download it. Am I missing a param?

// Load the SDK and UUID
var AWS = require('aws-sdk');
var uuid = require('node-uuid');

// Create an S3 client
var s3 = new AWS.S3();

var params = {
Bucket: "my-artifacts",
Key: "artifact.jar"
};
s3.getObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else     console.log(data);           // successful response
});

Response I get:

{ AcceptRanges: 'bytes',
LastModified: 2017-08-09T14:31:32.000Z,
ContentLength: 19442174,
ETag: '"0df85e32d56c37be82c99150824ca956-3"',
VersionId: 'xQBKHSPWAdd_JXzLOWv0AEixv4hSRDKK',
ContentType: 'application/java-archive',
Metadata: {},
Body: <Buffer 50 4b 03 04 14 00 08 08 08 00 ec 73 09 4b 00 00 00 00 00 
00 00 00 00 00 00 00 09 00 04 00 4d 45 54 41 2d 49 4e 46 2f fe ca 00 00 
03 00 50 4b 07 08 00 ... > }
isaac weathers
  • 1,436
  • 4
  • 27
  • 52
  • 1
    See the accepted answer in [this post](https://stackoverflow.com/questions/36942442/how-to-get-response-from-s3-getobject-in-node-js) - you have the data, you just need to save the body response. – stdunbar Aug 09 '17 at 16:38
  • Possible duplicate of [How to get response from S3 getObject in Node.js?](https://stackoverflow.com/questions/36942442/how-to-get-response-from-s3-getobject-in-node-js) – stdunbar Aug 09 '17 at 16:41
  • Yeah saw those but it seems a little convoluted. Was wondering if there was something as simple as specifiying a target like there is for Ruby: https://aws.amazon.com/blogs/developer/downloading-objects-from-amazon-s3-using-the-aws-sdk-for-ruby/ Theoretically, it is already getting the object and we should just be able to specify the location of where to put the object. Will read some more. Maybe I am overlooking something. – isaac weathers Aug 09 '17 at 17:04

1 Answers1

3

So looks like it is baked in with node using streams and this seems much simpler than the other answers I found:

// Load the SDK
var AWS = require('aws-sdk');
var fs = require('fs');

var s3 = new AWS.S3({apiVersion: '2006-03-01'});
var params = {
Bucket: "your-bucket",
Key: "your/dir/location/for/object.war",
};
var file = require('fs').createWriteStream('yourlocaldirectory/object.war');
s3.getObject(params).createReadStream().pipe(file);

Found here: http://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/requests-using-stream-objects.html

Using node v8.1.3

isaac weathers
  • 1,436
  • 4
  • 27
  • 52