Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
484 views
in Technique[技术] by (71.8m points)

node.js - 用于将文件从s3复制到s3存储桶的节点js代码(node js code for copying file from s3 to s3 bucket)

I want to take backup the file from one s3 bucket to another s3 bucket.

(我想将文件从一个s3存储桶备份到另一个s3存储桶。)

I'm using lambda function with nodejs for taking backup for dynamo DB.

(我正在将lambda函数与nodejs一起使用以为dynamo DB进行备份。)

So i want to copy the files from s3 to another s3 bucket.

(所以我想将文件从s3复制到另一个s3存储桶。)

Can anyone tell me the nodejs code for copying files from s3 to s3?

(谁能告诉我用于将文件从s3复制到s3的nodejs代码?)

  ask by Lakshminarayanan S translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

(希望它会有所帮助。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...