使用Lambda在多个区域中进行s3存储桶复制

时间:2019-06-18 09:06:07

标签: python amazon-web-services amazon-s3 lambda

用例:在同一AWS账户的多个区域中复制s3存储桶

此代码仅在一个区域中复制存储桶。我尝试在python print vikash之前添加此行python s3.copy_object,但是它仅重复一次。

import boto3
from botocore.exceptions import ClientError
import sys
import traceback
import re


def lambda_handler(event, context):
    print 'Recieved following event'
    print '--------------------------------------------------'
    print event
    print '--------------------------------------------------'
    try:
        # Obtain destination account Credentials



        bucks = ['bucket1-deploy-ap-northeast-1', 'bucket2-deploy-ap-south-1']

        s3 = boto3.client('s3')
        records = event['Records']
        print records
        for record in records:
            src_buck = record['s3']['bucket']['name']
            src_key = record['s3']['object']['key']
            if re.match('^ObjectCreated:*', record['eventName']):
                for des_buck in bucks:
                    print des_buck
                    print "vikash"

                    print s3.copy_object(ACL='bucket-owner-full-control', Bucket=des_buck, Key=src_key,
                                   CopySource={'Bucket': src_buck, 'Key': src_key}, StorageClass='STANDARD')
                    print 'Successfully copied %s to %s' % (src_key, des_buck)


            elif re.match('^ObjectRemoved:*', record['eventName']):
                for des_buck in bucks:
                    s3.delete_object(Bucket=des_buck, Key=src_key)
                    print 'Successfully deleted %s from %s' % (src_key, des_buck)





0 个答案:

没有答案