get last modified object from S3 CLI

后端 未结 5 1919
别跟我提以往
别跟我提以往 2020-12-04 09:49

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 o

5条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 10:17

    After a while there is a small update how to do it a bit elegant:

    aws s3api list-objects-v2 --bucket "my-awesome-bucket" --query 'sort_by(Contents, &LastModified)[-1].Key' --output=text
    

    Instead of extra reverse function we can get last entry from the list via [-1]

    Old answer:

    This command just do the job without any external dependencies:

    aws s3api list-objects-v2 --bucket "my-awesome-bucket" --query 'reverse(sort_by(Contents, &LastModified))[:1].Key' --output=text
    

提交回复
热议问题