从 S3 CLI 获取最后修改的对象 [英] get last modified object from S3 CLI

查看:31
本文介绍了从 S3 CLI 获取最后修改的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个用例,我以编程方式启动一个 EC2 实例,从 S3 复制和可执行文件,运行它并关闭实例(在用户数据中完成).我只需要从 S3 获取最后添加的文件.有没有办法使用 CLI 从 S3 存储桶中获取最后修改的文件/对象?

I have a use case where I programmatically bring up an EC2 instance, copy and executable file from S3, run it and shut down the instance (done in user-data). I need to get only the last added file from S3. Is there a way to get the last modified file / object from a S3 bucket using the CLI ?

推荐答案

您可以使用 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

这篇关于从 S3 CLI 获取最后修改的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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