How can I download updated files from the S3 bucket folder?

198    Asked by Aalapprabhakaran in AWS , Asked on Jan 2, 2024

I am currently working on a project that demands downloading specific files from an S3 bucket folder. However, the S3 bucket has hundreds of files and I need to retrieve only the files updated in the last 24 hours. How can I download these files from the S3 bucket folder? 

To download specific files from an S3 folder that are uploaded in the last 24 hours, you can use several points:-

By using the AWS command line interface (CLI):

Aws s3api list-objects-v2 –bucket YOUR_BUCKET_NAME –prefix YOUR_FOLDER_NAME/ --query “Contents[?LastModified > `$(date -v-1d -u +”%Y-%m-%dT%H:%M:%SZ”)`].Key” –output text | xargs -I {} aws s3 cp s3://YOUR_BUCKET_NAME/{} .
By using Boto3 (Python SDK for AWS):
Import boto3
Import datetime
Bucket_name = ‘YOUR_BUCKET_NAME’
Prefix = ‘YOUR_FOLDER_NAME/’
S3 = boto3.client(‘s3’)
Yesterday = datetime.datetime.utcnow() – datetime.timedelta(days=1)
Response = s3.list_objects_v2(
    Bucket=bucket_name,
    Prefix=prefix
)
For obj in response.get(‘Contents’, []):
    If obj.get(‘LastModified’) > yesterday:
        File_name = obj.get(‘Key’)
        S3.download_file(bucket_name, file_name, file_name)

You can replace YOUR_BUCKET_NAME and YOUR_FOLDER_NAME with your real or original S3 bucket and folder names respectively.



Your Answer

Interviews

Parent Categories