Question:
Is there any way to use boto3 to loop the bucket contents in two different buckets (source and target) and if it finds any key in source that does not match with target, it uploads it to the target bucket. please note I do not want to use aws s3 sync. I am currently using the following code for doing this job:
1 2 3 4 5 6 7 8 9 10 |
import boto3 s3 = boto3.resource('s3') src = s3.Bucket('sourcenabcap') dst = s3.Bucket('destinationnabcap') objs = list(dst.objects.all()) for k in src.objects.all(): if (k.key !=objs[0].key): # copy the k.key to target |
Answer:
If you only wish to compare by Key (ignoring differences within objects), you could use something like:
1 2 3 4 5 6 7 8 |
s3 = boto3.resource('s3') source_bucket = s3.Bucket('source') destination_bucket = s3.Bucket('destination') destination_keys = [object.key for object in destination_bucket.objects.all()] for object in source_bucket.objects.all(): if (object.key not in destination_keys): # copy object.key to destination |