You can use the awscli to test for the presence of an object in S3. For example:
aws s3api head-object --bucket mybucket --key dogs/snoopy.png
You will get a JSON response and a zero returncode if the object exists (and you have permission to issue HeadObject against it).
Here's an example bash script:
#!/bin/bash
aws s3api head-object --bucket mybucket --key dogs/snoopy.png 1>/dev/null 2>&1
if [[ $? -eq 0 ]]; then
echo "S3 object exists"
else
echo "S3 object does not exist"
fi
To test if there are objects under a given S3 prefix such as dogs/, you can issue:
aws s3 ls mybucket/dogs/ --recursive | grep -v "\/$" | wc -l
The output will be the count of S3 objects, for example 0 or 2. You can capture this in the normal way, for example:
COUNT=$(aws s3 ls mybucket/dogs/ --recursive | grep -v "\/$" | wc -l | tr -d ' ')
if [[ $COUNT -eq "0" ]]; then
echo "No objects under dogs/"
else
echo "Some ($COUNT) objects under dogs/"
fi
If using an AWS credentials profile other than your default, append --profile myprofile to the awscli command.