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

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

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