Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy file from the s3 to the local?

I have a lot of files in my s3 bucket, so is there any aws cli command which I can use to find a most recent file with a prefix name in s3? and how can I copy that file from s3 to my local folder? Can I use Boto3 or python library to do this?


1 Answers

This command will list the 'latest' object for a given prefix:

aws s3api list-objects --bucket MY-BUCKET --prefix foo/ --query 'sort_by(Contents, &LastModified)[-1].Key' --output text

You could combine it with a copy command:

key=$(aws s3api list-objects --bucket MY-BUCKET --prefix foo/ --query 'sort_by(Contents, &LastModified)[-1].Key' --output text)
aws s3 cp s3://MY-BUCKET/$key .

The --query parameter is very powerful. See: JMESPath Tutorial

like image 200
John Rotenstein Avatar answered Sep 15 '25 00:09

John Rotenstein