How to check with AWS cli if file exists in S3
Today i was working on adding history trends feature from Allure report to our Integration API Tests Framework and in order to do that i need to implement simple algorithm:
- Check if folder for specific group of tests history exists
- If it exists, copy this folder to allure-results folder
- Generate Report with
./gradlew allureReport
command - Sync new history folder with S3 bucket
Seems easy, right ? They only problem i faced was that S3 doesn't provide you with an easy option to check if object in bucket exists. So, after some googling and try outs i managed to create this simple script :
totalFoundObjects=$(aws s3 ls s3://${S3_BUCKET_NAME}/${PATH_TO_FOLDER} --recursive --summarize | grep "Total Objects: " | sed 's/[^0-9]*//g')
if [ "$totalFoundObjects" -eq "0" ]; then
echo "There are no files found"
else
echo "I have found $totalFoundObjects"
fi
Hope this will be helpful for you as well.