How to check if a key exists in s3 bucket using boto3?
s3 = boto3.resource(‘s3′)
except botocore.exceptions.ClientError as e:
if e.response[‘Error’][‘Code’] == “404”:
# The object does not exist.
# Something else has gone wrong.
# The object does exist.Load() does a HEAD request for a single key, which is fast, even if the object in question is large or you have many objects in your bucket.
Of course, you might be checking if the object exists because you are planning on using it. If that is the case, you can just forget about the load() and do a get() or download_file() directly, then handle the error case.