0 votes
1 view
in AWS by (8.9k points)

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 ?

1 Answer

0 votes
by (18.5k points)

Yes, using the following commands in the CLI you can get the last modified objects in the bucket:

Listing all the objects in the bucket:

$ aws s3 ls $BUCKET --recursive

Note: The objects in the displayed list will be sorted alphabetically by key. The first column that you will see will be the column of the modified time.

Sorting the objects displayed in the list as per the last modified time:

$ aws s3 ls $BUCKET --recursive | sort

Note: The last row will display the last modified object along with its respective modified time.

To select the last row and extract the name of the object from that row:

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

  • 'tail -n' selects the last row
  • awk '{print $4}' extracts the 4th column in the last row, which is the name of the object.

To download the extracted object:

$ aws s3 cp s3://$BUCKET/$KEY ./latest-object

Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...