简体   繁体   中英

getObject from s3 using aws-sdk and store locally

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 ... > }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM