refactor multiple instances of S3 key check logic to single variable