Boto 2 boto.s3.key.Key an object used to use the exists method, which checks if a key existed on S3 by executing a HEAD request and looking at the result, but it seems that this is no longer there. You have to do it yourself:
import boto3 import botocore s3 = boto3.resource('s3') try: s3.Object('my-bucket', 'dootdoot.jpg').load() except botocore.exceptions.ClientError as e: if e.response['Error']['Code'] == "404":
load() executes 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 can check if an object exists because you plan to use it. If so, you can just forget about load() and do get() or download_file() directly, and then handle the error case there.
Wander Nauta Nov 21 '15 at 11:53 2015-11-21 11:53
source share