skip to Main Content

I currently have a bat file that copies the last modified file from AWS S3 bucket to local folder.

for /f "delims=" %%i in ('aws s3api list-objects-v2 --bucket example.sftp --prefix data/ --query "sort_by(Contents, &LastModified)[-1].Key" --output text') do set object=%%i
aws s3 cp s3://example.sftp/%object% E:DATA_S3

I want to modify this to get two files when sorted by last modified instead of one. Changing [-1] to [-2] is not working.

2

Answers


  1. JMESPath uses Python syntax for referencing lists, so use:

    --query "sort_by(Contents, &LastModified)[-2:].Key"
    

    This means "from the second last element to the end of the list".

    I highly recommend the JMESPath Tutorial, which includes interactive testing of commands. I tested this concept using that page.

    Login or Signup to reply.
  2. You can use a JMESPath query to get the two most recent items using [-2:], and operate on each one in turn. Note that in the path, instead of using .Key, use .[Key], which causes AWS CLI’s text output to delimit with newlines instead of tabs, allowing for easy parsing by the for operator of the batch file:

    for /f "delims=" %%i in ('aws s3api list-objects-v2 --bucket example-bucket --prefix target/prefix/ --query "sort_by(Contents, &LastModified)[-2:].[Key]" --output text') do (
        aws s3 cp s3://example-bucket/%%i foldertocopyto
    )
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search