在同一亚马逊帐户的存储桶之间移动文件

Moving file between buckets on same amazon account

我在使用 lambda 触发器在 amazon 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');
    });
});

如果您只是在存储桶之间移动文件,您可以简单地使用 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 内部 "move" 文件,这比下载和上传文件要快得多。此外,您只需支付 PUT 请求费用,无需支付任何数据传输费用。