Need Sync S3 When S3 Have New Objects Put In.

How To

Before Start

Create Source And Destination Bucket First

In This Demo, Just Create Two Buckets Name : wei-sync-bucket-1 & wei-sync-bucket-2

—Attention : If Just Demo Or Practice, Please Set Bucket As Same Region, Cause When You Transfer Object Cross Region, You’ll Be Charged Fee—

Then Go To Lambda Function And Create Function

You Need Add Trigger (From S3), Write Simple Python code.

  • Add Trigger

  • Write code

import boto3
s3 = boto3.resource('s3')

def lambda_handler(event, context):
    Get SRC Bucket Objects
    srcBucket = s3.Bucket('wei-sync-bucket-1')
    getObjects = srcBucket.objects.all()
    Copy All Objects From SRC Bucket To DES Bucket 
    for cpOBJ in getObjects:
        copy_source = {
            'Bucket' : 'wei-sync-bucket-1',
            'Key' : cpOBJ.key
        bucket = s3.Bucket('wei-sync-bucket-2')
        bucket.copy(copy_source, cpOBJ.key)
    return None