I'm my S3 bucket there are so many files are in different file formats. So I would like to copy from all the subfolders which has .JSON extension to another folder.
Current Structure:
S3://mybucket/f1/file.JPG
S3://mybucket/f1/newfile.JSON
S3://mybucket/f2/Oldfile.JSON
It (JSON FILES) should be copied to the folder arrange:
S3://mybucket/arrange/newfile.JSON
S3://mybucket/arrange/Oldfile.JSON
I tried this (But there is not filter for JSON) From stackoverflow
import os
import boto3
old_bucket_name = 'SRC'
old_prefix = 'A/B/C/'
new_bucket_name = 'TGT'
new_prefix = 'L/M/N/'
s3 = boto3.resource('s3')
old_bucket = s3.Bucket(old_bucket_name )
new_bucket = s3.Bucket(new_bucket_name )
for obj in old_bucket.objects.filter(Prefix=old_prefix):
old_source = { 'Bucket': old_bucket_name,
'Key': obj.key}
# replace the prefix
new_key = obj.key.replace(old_prefix, new_prefix)
new_obj = new_bucket.Object(new_key)
new_obj.copy(old_source)
You can keep a filter for JSON files like below :
for obj in old_bucket.objects.filter(Prefix=old_prefix):
if obj.key.endswith('.JSON'):
old_source = { 'Bucket': old_bucket_name,
'Key': obj.key}
# replace the prefix
new_key = obj.key.replace(old_prefix, new_prefix)
new_obj = new_bucket.Object(new_key)
new_obj.copy(old_source)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With