使用AWS CLI在S3存储桶中下载最新文件? [英] Downloading the latest file in an S3 bucket using AWS CLI?

查看:346
本文介绍了使用AWS CLI在S3存储桶中下载最新文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个S3存储桶,其中包含数据库备份.我正在创建一个脚本,我想下载最新的备份(并最终将其还原到其他地方),但是我不确定如何只从存储桶中获取最新的文件.

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.

是否可以使用AWS CLI工具将最新文件从s3存储桶复制到本地目录?

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

推荐答案

这是您可以采用的方法.

This is a approach you can take.

您可以使用aws s3 ls $BUCKET --recursive列出存储桶中的所有对象:

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

它们是按字母顺序排序的,但是第一列是最后修改的时间.快速的sort将按日期对它们重新排序:

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选择最后一行,awk '{print $4}'提取第四列(对象的名称).

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

最后但并非最不重要的一点是,将其放入aws s3 cp以下载对象:

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

这篇关于使用AWS CLI在S3存储桶中下载最新文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆