Help us understand the problem. What is going on with this article?

boto3の例外ハンドリング

More than 1 year has passed since last update.

boto3を使ってるときに、存在しないS3のオブジェクトを読み込んだ時のエラーハンドリングがしたかった時に調べたことをメモ。

NoSuchKeyのエラーが返るけど、動的に生成される例外クラスのようで、インポートしてexcept節に記述できない。

stackoverflowに対応方法が書いてた。
http://stackoverflow.com/questions/33068055/boto3-python-and-how-to-handle-errors

以下、抜粋。

import boto3
from botocore.exceptions import ClientError

try:
    iam = boto3.client('iam')
    user = iam.create_user(UserName='fred')
    print "Created user: %s" % user
except ClientError as e:
    if e.response['Error']['Code'] == 'EntityAlreadyExists':
        print "User already exists"
    else:
        print "Unexpected error: %s" % e

botocore.exceptionsのClientErrorとして例外をキャッチして、response['Error']['Code']の値でエラー内容を確認する。

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away