我在lambda触发器中移动亚马逊s3上的文件时遇到了一些问题。
我可以做s3.getObject
,但我认为我的问题是我在做s3.putObject
var srcBucket = event.Records[0].s3.bucket.name;
var srcKey = decodeURIComponent(event.Records[0].s3.object.key.replace(/\+/g, " "));
var s3params = {Bucket: srcBucket, Key: srcKey};
s3.getObject(s3params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("Loaded " + data.ContentLength + " bytes"); // successful response
var destinationpath = destinationbucket + '/moved_files/';
console.log("Destination: " +destinationpath);
var destiniation_name = destinationpath + str.split("/")[3];
console.log(destiniation_name);
var upparams = {Bucket: destinationbucket, Key: destiniation_name,ContentType: data.ContentType, Body: data.Stream};
s3.putObject(upparams,function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log("upload response " +data); // successful response
context.succeed('exit');
});
});
答案 0 :(得分:4)
如果您只是在存储桶之间移动文件,则可以使用S3.copyObject
:
var params = {
CopySource: srcBucket + '/' + srcKey,
Bucket: dstBucket,
Key: dstKey
};
s3.copyObject(params, function(err, data) {
if (err) console.log(err, err.stack); // an error occurred
else console.log(data); // successful response
});
这将在S3内部“移动”文件,这将比下载和上传文件更快。此外,您只需支付PUT
请求,无需进行数据传输。