Check if file exists in S3 Bucket
If you do aws s3 ls
on the actual filename. If the filename exists, the exit code will be 0 and the filename will be displayed, otherwise, the exit code will not be 0:
aws s3 ls s3://bucket/filname
if [[ $? -ne 0 ]]; then
echo "File does not exist"
fi
first answer is close but in cases where you use -e in shebang, the script will fail which you would most like not want. It is better to use wordcount. So you can use the below command:
wordcount=`aws s3 ls s3://${S3_BUCKET_NAME}/${folder}/|grep $${file}|wc -c`
echo wordcount=${wordcount}
if [[ "${wordcount}" -eq 0 ]]; then
do something
else
do something
fi
Try the following :
aws s3api head-object --bucket ${S3_BUCKET} --key ${S3_KEY}
It retrieves the metadata of the object without retrieving the object itself. READ(s3:GetObject) access is required. .