简体   繁体   English

使用aws-sdk从s3中获取getObject并在本地存储

[英]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. 调用getObject时,似乎找不到在哪里指定对象的位置。

Basically, trying to get an object from s3 and download it. 基本上,尝试从s3获取对象并下载它。 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 在这里找到: http : //docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/requests-using-stream-objects.html

Using node v8.1.3 使用节点v8.1.3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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