check if a key exists in a bucket in s3 using boto3 check if a key exists in a bucket in s3 using boto3 python python

check if a key exists in a bucket in s3 using boto3


Boto 2's boto.s3.key.Key object used to have an exists method that checked if the key existed on S3 by doing a HEAD request and looking at the the result, but it seems that that no longer exists. You have to do it yourself:

import boto3import botocores3 = boto3.resource('s3')try:    s3.Object('my-bucket', 'dootdoot.jpg').load()except botocore.exceptions.ClientError as e:    if e.response['Error']['Code'] == "404":        # The object does not exist.        ...    else:        # Something else has gone wrong.        raiseelse:    # 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're 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 there.


The easiest way I found (and probably the most efficient) is this:

import boto3from botocore.errorfactory import ClientErrors3 = boto3.client('s3')try:    s3.head_object(Bucket='bucket_name', Key='file_path')except ClientError:    # Not found    pass


I'm not a big fan of using exceptions for control flow. This is an alternative approach that works in boto3:

import boto3s3 = boto3.resource('s3')bucket = s3.Bucket('my-bucket')key = 'dootdoot.jpg'objs = list(bucket.objects.filter(Prefix=key))if any([w.key == path_s3 for w in objs]):    print("Exists!")else:    print("Doesn't exist")