Introduction

In this tutorial, we will look at a few different ways how you can check if a particular file exists in S3.

s3 ls

You can use the following command to check if a file exists in an S3 bucket:

aws s3 ls s3://bucket-name/path/to/file

If the file exists, the command will return its metadata. If the file does not exist, the command will return an error message.

However, this command will not work if you have multiple files with the same prefix. For example, if you have the following files in your bucket:

s3://bucket-name/path/to/file.txt
s3://bucket-name/path/to/file1.txt

If you run the following command:

aws s3 ls s3://bucket-name/path/to/file

The command will return the metadata of both files. This is because the command will return all files that have the prefix path/to/file.

s3api head-object

You can use the s3api head-object command to check if a file exists in S3. This command will return the metadata of the file if it exists. If the file does not exist, the command will return an error message. s3api head-object is the recommended way to check if a file exists in S3.

aws s3api head-object --bucket bucket-name --key path/to/file

How to use the command in a bash script?

If you want to use this command in a bash script, you can use the following steps:


aws s3api head-object --bucket bucket-name --key path/to/file || NOT_EXIST=true
if [ $NOT_EXIST ]; then
  echo "File does not exist."
  exit 1
fi

Conclusion

In this tutorial, we looked at how we can use the AWS S3 CLI to check if a file exists in S3. We looked at two different commands and when to use them.