You can try the following code in lambda:
(您可以在lambda中尝试以下代码:)
// Load the AWS SDK
const aws = require('aws-sdk');
const s3 = new aws.S3();
// Define 2 new variables for the source and destination buckets
var srcBucket = "YOUR-SOURCE-BUCKET";
var destBucket = "YOUR-DESTINATION-BUCKET";
var sourceObject = "YOUR-SOURCE-OBJECT";
//Main function
exports.handler = (event, context, callback) => {
//Copy the current object to the destination bucket
http://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/S3.html#copyObject-property
s3.copyObject({
CopySource: srcBucket + '/' + sourceObject,
Bucket: destBucket,
Key: sourceObject
}, function(copyErr, copyData){
if (copyErr) {
console.log("Error: " + copyErr);
} else {
console.log('Copied OK');
}
});
callback(null, 'All done!');
};
And Attach the following Policy to your IAM Role attached in Lambda:
(并将以下策略附加到Lambda中附加的IAM角色:)
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListSourceAndDestinationBuckets",
"Effect": "Allow",
"Action": [
"s3:ListBucket",
"s3:ListBucketVersions"
],
"Resource": [
"arn:aws:s3:::YOUR-SOURCE-BUCKET",
"arn:aws:s3:::YOUR-DESTINATION-BUCKET"
]
},
{
"Sid": "SourceBucketGetObjectAccess",
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:GetObjectVersion"
],
"Resource": "arn:aws:s3:::YOUR-SOURCE-BUCKET/*"
},
{
"Sid": "DestinationBucketPutObjectAccess",
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": "arn:aws:s3:::YOUR-DESTINATION-BUCKET/*"
}
]
}
Hope it might help.
(希望它会有所帮助。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…