gpt4 book ai didi

node.js - 用于将文件从 s3 复制到 s3 存储桶的 Node js 代码

转载 作者:搜寻专家 更新时间:2023-11-01 00:49:46 26 4
gpt4 key购买 nike

我想将文件从一个 s3 存储桶备份到另一个 s3 存储桶。我在 nodejs 中使用 lambda 函数来备份 dynamo DB。所以我想将文件从 s3 复制到另一个 s3 存储桶。谁能告诉我将文件从 s3 复制到 s3 的 nodejs 代码?

最佳答案

您可以在 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!');
};

并将以下策略附加到您在 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/*"
}
]
}

关于node.js - 用于将文件从 s3 复制到 s3 存储桶的 Node js 代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53759852/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com