marjun
marjun

Reputation: 726

AWS s3api command to get list object with lastmodified greater than yyyy-mm-dd HH:MM:SS

I'm Trying to fetch the objects list from the S3 bucket which are uploaded recently. But only Contents[?LastModified>='yyyy-mm-hh'] comparision is working in query. When I tried with Contents[?LastModified>='yyyy-mm-hh HH:MM:SS'] then its comparing only yyyy-mm-dd giving the list which has been updated in that day and when i tried to fetch files which has added recently with timestamp HH:MM:SS its giving all the objects added in that day.

echo "###################### Previous Run : $previous_run"
dat2=$(date -d "$previous_run" "+%Y-%m-%d %H:%M:%S")

echo $dat2

get_Latest_Files()
{
        #get new files from s3
        json_var=$(aws s3api list-objects --bucket "$input_bucket" --prefix "$input_prefix" --query "Contents[?LastModified>='$dat2'].{Key: Key,LastModified: LastModified}" --output text)

        echo "$json_var"
        if [ -z "$json_var" ]
        then
            echo "No latest files to Process...!"
            exit
        else
                #grep for tgz files
                echo $json_var  | tr " " "\n" | egrep -i "(\.tgz)|(\.tar\.gz)$" | awk -v prefix="s3://$input_bucket/" '{print prefix $0}' > input_files.txt
                cat input_files.txt
        fi
}

Upvotes: 0

Views: 1901

Answers (1)

RajNarayan Sett
RajNarayan Sett

Reputation: 11

Solution:

Try to use this format of the date:

"Contents[?LastModified>='2019-07-26T17:49:00.000Z'][].{Key: Key,LastModified: LastModified}"

Upvotes: 1

Related Questions