'Is there a way to move all files with specific file extensions from one folder to another in S3 using Boto3 python?

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) ?



Solution 1:[1]

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.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 John Rotenstein