-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathsync_buckets.py
More file actions
48 lines (34 loc) · 1.28 KB
/
sync_buckets.py
File metadata and controls
48 lines (34 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
"""Copy objects from one S3 bucket to another."""
import optparse
import boto3
s3 = boto3.resource("s3")
def sync_buckets(src_bucket, dest_bucket):
"""Sync objects from one AWS S3 bucket to another.
Args:
src_bucket (boto3 Bucket): Objects will be copied from this bucket
to *dest_bucket*.
dest_bucket (boto3 Bucket): Objects will be copied here from *src_bucket*.
Returns:
int: Count of objects copied between buckets.
"""
count = 0
for src_obj in src_bucket.objects.all():
response = src_obj.get()
dest_bucket.put_object(Key=src_obj.key, Body=response["Body"].read())
count += 1
return count
def sync_buckets_by_name(src_bucket_name, dest_bucket_name):
"""Sync objects from one AWS S3 bucket to another by name."""
src_bucket = s3.Bucket(src_bucket_name)
dest_bucket = s3.Bucket(dest_bucket_name)
return sync_buckets(src_bucket, dest_bucket)
def main():
parser = optparse.OptionParser(
description="Copy objects from one S3 bucket to another.",
usage="usage: %prog [options] src_bucket dest_bucket",
)
options, args = parser.parse_args()
src_bucket, dest_bucket = args
sync_buckets_by_name(src_bucket, dest_bucket)
if __name__ == "__main__":
main()