Downloading the latest file in an S3 bucket using

2020-02-24 12:12发布

I have an S3 bucket that contains database backups. I am creating a script that I would like to download the latest backup (and eventually restore it somewhere else), but I'm not sure how to go about only grabbing the most recent file from a bucket.

Is it possible to copy only the most recent file from a s3 bucket to a local directory using AWS CLI tools?

4条回答
▲ chillily
2楼-- · 2020-02-24 12:25

And here is a bash script create based on @error2007s's answer. This script requires your aws profile and bucket name as variables, and downloads the latest object to your ~/Downloads folder:

#!/bin/sh
PROFILE=your_profile
BUCKET=your_bucket

OBJECT="$(aws s3 ls --profile $PROFILE $BUCKET --recursive | sort | tail -n 1 | awk '{print $4}')"
aws s3 cp s3://$BUCKET/$OBJECT ~/Downloads/$OBJECT --profile $PROFILE
查看更多
狗以群分
3楼-- · 2020-02-24 12:32

The Above solutions are Bash for if one want to do the same thing in Powershell for downloading in windows using the follwing script :

$s3location = 's3://bucket/'
$filename=C:\Progra~1\Amazon\AWSCLI\aws.exe s3 ls s3://bucket/PROD_FULL/ -- 
recursive | sort |select -last 3 
$Dpath='I:\Data_S3'

foreach($files in $filename)
{
#$files.ToString()
$testpath1 = Split-Path $path -leaf
$testpath1
$path=$s3location+$files

C:\Progra~1\Amazon\AWSCLI\aws.exe s3 cp $path $Dpath
echo(" ***Files Downloaded ***")

}
查看更多
Juvenile、少年°
4楼-- · 2020-02-24 12:33

This is a approach you can take.

You can list all the objects in the bucket with aws s3 ls $BUCKET --recursive:

$ aws s3 ls $BUCKET --recursive
2015-05-05 15:36:17          4 an_object.txt
2015-06-08 14:14:44   16322599 some/other/object
2015-04-29 12:09:29      32768 yet-another-object.sh

They're sorted alphabetically by key, but that first column is the last modified time. A quick sort will reorder them by date:

$ aws s3 ls $BUCKET --recursive | sort
2015-04-29 12:09:29      32768 yet-another-object.sh
2015-05-05 15:36:17          4 an_object.txt
2015-06-08 14:14:44   16322599 some/other/object

tail -n 1 selects the last row, and awk '{print $4}' extracts the fourth column (the name of the object).

$ aws s3 ls $BUCKET --recursive | sort | tail -n 1 | awk '{print $4}'
some/other/object

Last but not least, drop that into aws s3 cp to download the object:

$ KEY=`aws s3 ls $BUCKET --recursive | sort | tail -n 1 | awk '{print $4}'`
$ aws s3 cp s3://$BUCKET/$KEY ./latest-object
查看更多
爱情/是我丢掉的垃圾
5楼-- · 2020-02-24 12:52
FILE=`aws s3api list-objects-v2 --bucket "$BUCKET_NAME" --query 'reverse(sort_by(Contents[?contains(Key, \`$FILE_NAME_FILTER\`)], &LastModified))[:1].Key' --output=text`;aws s3 cp "s3://$BUCKET_NAME/$FILE" .

$BUCKET_NAME - is the bucket from which you want to download.

$FILE_NAME_FILTER - a string used as a filter for the name, which you want to match.

aws s3 cp " " - its in double-quotes because to also include files that have spaces in their names.

查看更多
登录 后发表回答