要实现AWS Lambda函数在不同账户之间进行S3到S3复制,需要配置跨账户访问和复制策略。以下是一个基本的解决方案,包含了Lambda函数的代码示例。
import boto3
def lambda_handler(event, context):
source_bucket = event['Records'][0]['s3']['bucket']['name']
source_key = event['Records'][0]['s3']['object']['key']
destination_bucket = '目标账户的目标桶名称'
destination_key = '目标桶中的目标对象键'
s3 = boto3.client('s3')
copy_source = {'Bucket': source_bucket, 'Key': source_key}
s3.copy_object(CopySource=copy_source, Bucket=destination_bucket, Key=destination_key)
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:GetObject",
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::源桶名称/*"
]
}
]
}
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"s3:PutObject"
],
"Resource": [
"arn:aws:s3:::目标桶名称/*"
]
}
]
}
import boto3
def lambda_handler(event, context):
source_bucket = event['Records'][0]['s3']['bucket']['name']
source_key = event['Records'][0]['s3']['object']['key']
destination_bucket = '目标桶名称'
destination_key = source_key
s3 = boto3.client('s3')
copy_source = {'Bucket': source_bucket, 'Key': source_key}
s3.copy_object(CopySource=copy_source, Bucket=destination_bucket, Key=destination_key)
通过以上步骤,你可以实现在不同账户之间进行S3到S3的复制操作。请注意,上述示例中的角色策略仅提供了最小的权限,你可以根据实际需求进行调整。