[PYTHON] How to catch boto3 S3 NoSuchKey error

The boto3 documentation states that when getting an S3 object in Python's boto3, the error S3.Client.exceptions.NoSuchKey occurs if the object does not exist. However, I didn't know how to catch this error in Python, so I tried it and investigated it.

For some reason, the behavior of get_object and head_object was different.

get_object

I was able to catch the error with the following two patterns of code.

import boto3

s3_bucket = "..."
s3_key = "..."

session = boto3.session.Session()
s3_client = session.client("s3")

try:
    s3_client.get_object(
        Bucket = s3_bucket,
        Key = s3_key,
    )
except s3_client.exceptions.NoSuchKey as e:
    "NOT FOUND ERROR!"
import boto3
import botocore.exceptions

s3_bucket = "..."
s3_key = "..."

session = boto3.session.Session()
s3_client = session.client("s3")

try:
    s3_client.get_object(
        Bucket = s3_bucket,
        Key = s3_key,
    )
except botocore.exceptions.ClientError as e:
    if e.response["Error"]["Code"] == "NoSuchKey":
        "NOT FOUND ERROR!"
    else:
        raise

head_object

I was able to catch the error with the following code.

I couldn't even try s3_client.exceptions.NoSuchKey by imitating get_object.

The catch with ClientError is also different from get_object for some reason because the string of Code is.

import boto3
import botocore.exceptions

s3_bucket = "..."
s3_key = "..."

session = boto3.session.Session()
s3_client = session.client("s3")

try:
    s3_client.head_object(
        Bucket = s3_bucket,
        Key = s3_key,
    )
except botocore.exceptions.ClientError as e:
    if e.response["Error"]["Code"] == "404":
        "NOT FOUND ERROR!"
    else:
        raise

version information

$ pip list | grep boto 
boto3                   1.14.63
botocore                1.17.63

Recommended Posts

How to catch boto3 S3 NoSuchKey error
How to deal with SSL error when connecting to S3 with boto of Python
Use boto3 to mess with S3
How to suppress display error in matplotlib
Use boto to upload / download files to s3.
How to get the key on Amazon S3 with Boto 3, implementation example, notes
[Go] How to create a custom error for Sentry
[Python] Local → Procedure for uploading files to S3 (boto3)
[Python] How to deal with pandas read_html read error
How to install poetry (error handling) in zsh environment
How to use xml.etree.ElementTree
How to use Python-shell
How to use tf.data
How to use virtualenv
Scraping 2 How to scrape
How to use Seaboan
How to use image-match
How to use shogun
How to install Python
How to read PyPI
How to install pip
How to use Virtualenv
How to use numpy.vectorize
How to update easy_install
How to install archlinux
How to use pytest_report_header
How to restart gunicorn
How to virtual host
How to debug selenium
How to use partial
How to use Bio.Phylo
How to read JSON
How to use SymPy
How to use x-means
How to use WikiExtractor.py
How to update Spyder
How to install BayesOpt
How to use virtualenv
How to use Matplotlib
How to use iptables
How to use numpy
How to use TokyoTechFes2015
How to use venv
How to use dictionary {}
How to use Pyenv
How to grow dotfiles
S3 uploader with boto
How to use list []
How to use python-kabusapi
"How to count Fukashigi"
How to install Nbextensions
How to use OptParse
How to use return
How to install Prover9
How to use dotenv
How to operate NumPy
How to use pyenv-virtualenv
How to use Go.mod
How to use imutils
How to use import
[AWS] How to deal with "Invalid codepoint" error in CloudSearch