How to check file update on an S3 bucket and download latest version into the local folder

engineering August 18, 2020
How to check file update on an S3 bucket and download latest version into the local folder Rez Moss

Rez Moss

@rezmos1

1- Install AWS cli on your server.

2- Run aws configure command to add your AWS Key & secret

3- Run aws s3 ls command to make sure you have permission to access to your S3 bucket and your AWS cli working properly

4- Save below code into a bash script file

#!/bin/bash

aws s3 ls s3://MY-BUCKET > a.txt     #get files list in S3 bucket
A=$(cat a.txt| md5sum | cut -d " " -f1)  #get md5 checksum for a.txt
B=$(cat b.txt| md5sum | cut -d " " -f1)  #get md5 checksum for b.txt


if [[ $A == $B ]]   #If A==B, which means they are equal
then
echo "Nothing to update => " $(date +"%d-%m-%Y-%H-%M-%S") >> logs.txt
else
echo "We should update"
    rm -rf b.txt 
    rm -rf updatedPkg.zip

    aws s3 cp s3://MY-BUCKET/updatedPkg.zip ./updatedPkg.zip  #Copy new file from S3 to local

    cp a.txt b.txt  # overwrite b.txt with a.txt content

    echo "last update => " $(date +"%d-%m-%Y-%H-%M-%S") >> logs.txt
fi

5- You can easily put above code to cron

Need expert advice? Schedule your AWS consultation today!

Schedule Now

Want to read more?

Back to Blog