I'm trying to move all files of specific file extensions from one folder to another using the S3 API.
The code I'm using to move files is:
s3_resource = boto3.resource(‘s3’)
s3_resource.Object(“bucket_name”, “newpath/to/object_B.txt”).copy_from(
CopySource=”path/to/your/object_A.txt”)
s3_resource.Object(“bucket_name”, “path/to/your/object_A.txt”).delete()
Is there a way to modify this to move just the files that have specific extensions (e.g. .txt
and .csv
) ?
It would be something like:
import boto3
s3_resource = boto3.resource('s3')
bucket = s3_resource.Bucket('bucket-name')
for object in bucket.objects.filter(Prefix='source_path/'):
if object.key.endswith('.txt') or object.key.endswith('.csv'):
bucket.Object(...).copy(...)
object.delete(...)
You will need to modify the target key to include the destination path.