标签归档:botocore

如何捕获botocore的NoSuchKey异常?

问题:如何捕获botocore的NoSuchKey异常?

我正在尝试编写“好”的python并捕获一个S3,但没有这样的关键错误:

session = botocore.session.get_session()
client = session.create_client('s3')
try:
    client.get_object(Bucket=BUCKET, Key=FILE)
except NoSuchKey as e:
    print >> sys.stderr, "no such key in bucket"

但是NoSuchKey尚未定义,我无法将其跟踪到需要定义的导入。

e.__class__botocore.errorfactory.NoSuchKeyfrom botocore.errorfactory import NoSuchKey给出了一个错误,并且from botocore.errorfactory import *也不起作用,我也不想捕获一个通用错误。

I’m trying to write “good” python and capture a S3 no such key error with this:

session = botocore.session.get_session()
client = session.create_client('s3')
try:
    client.get_object(Bucket=BUCKET, Key=FILE)
except NoSuchKey as e:
    print >> sys.stderr, "no such key in bucket"

But NoSuchKey isn’t defined and I can’t trace it to the import I need to have it defined.

e.__class__ is botocore.errorfactory.NoSuchKey but from botocore.errorfactory import NoSuchKey gives an error and from botocore.errorfactory import * doesn’t work either and I don’t want to capture a generic error.


回答 0

from botocore.exceptions import ClientError

try:
    response = self.client.get_object(Bucket=bucket, Key=key)
    return json.loads(response["Body"].read())
except ClientError as ex:
    if ex.response['Error']['Code'] == 'NoSuchKey':
        logger.info('No object found - returning empty')
        return dict()
    else:
        raise
from botocore.exceptions import ClientError

try:
    response = self.client.get_object(Bucket=bucket, Key=key)
    return json.loads(response["Body"].read())
except ClientError as ex:
    if ex.response['Error']['Code'] == 'NoSuchKey':
        logger.info('No object found - returning empty')
        return dict()
    else:
        raise

回答 1

使用botocore 1.5,客户端句柄似乎公开了异常类:

session = botocore.session.get_session()
client = session.create_client('s3')
try:
    client.get_object(Bucket=BUCKET, Key=FILE)
except client.exceptions.NoSuchKey as e:
    print >> sys.stderr, "no such key in bucket"

Using botocore 1.5, it looks like the client handle exposes the exception classes:

session = botocore.session.get_session()
client = session.create_client('s3')
try:
    client.get_object(Bucket=BUCKET, Key=FILE)
except client.exceptions.NoSuchKey as e:
    print >> sys.stderr, "no such key in bucket"

回答 2

在boto3中,我能够在资源的元客户端中访问异常。

import boto3

s3 = boto3.resource('s3')
s3_object = s3.Object(bucket_name, key)

try:
    content = s3_object.get()['Body'].read().decode('utf-8')
except s3.meta.client.exceptions.NoSuchKey:
    print("no such key in bucket")

In boto3, I was able to access the exception in resource’s meta client.

import boto3

s3 = boto3.resource('s3')
s3_object = s3.Object(bucket_name, key)

try:
    content = s3_object.get()['Body'].read().decode('utf-8')
except s3.meta.client.exceptions.NoSuchKey:
    print("no such key in bucket")

回答 3

我认为最完美的方式是在Boto3中

session = botocore.session.get_session()
client = session.create_client('s3')

try:
    client.get_object(Bucket=BUCKET, Key=FILE)
except client.exceptions.NoSuchKey:
    print("no such key in bucket")

有关错误处理的文档似乎很少,但是以下内容打印了适用的错误代码:

session = botocore.session.get_session()
client = session.create_client('s3')
try:
    try:
        client.get_object(Bucket=BUCKET, Key=FILE)
    except client.exceptions.InvalidBucketName:
        print("no such key in bucket")
except AttributeError as err:
    print(err)

<位于0x105e08c50的botocore.errorfactory.S3Exceptions对象>对象没有属性’InvalidBucketName’。有效的exceptions是:BucketAlreadyExists,BucketAlreadyOwnedByYou,NoSuchBucket,NoSuchKey,NoSuchUpload,ObjectAlreadyInActiveTierError,ObjectNotInActiveTierError

I think the most elegant way to do this is in Boto3 is

session = botocore.session.get_session()
client = session.create_client('s3')

try:
    client.get_object(Bucket=BUCKET, Key=FILE)
except client.exceptions.NoSuchKey:
    print("no such key in bucket")

The documentation on error handling seems sparse, but the following prints the error codes this works for:

session = botocore.session.get_session()
client = session.create_client('s3')
try:
    try:
        client.get_object(Bucket=BUCKET, Key=FILE)
    except client.exceptions.InvalidBucketName:
        print("no such key in bucket")
except AttributeError as err:
    print(err)

< botocore.errorfactory.S3Exceptions object at 0x105e08c50 > object has no attribute ‘InvalidBucketName’. Valid exceptions are: BucketAlreadyExists, BucketAlreadyOwnedByYou, NoSuchBucket, NoSuchKey, NoSuchUpload, ObjectAlreadyInActiveTierError, ObjectNotInActiveTierError


Security Monkey监控AWS、GCP、OpenStack和GitHub组织的资产及其随时间的变化

Security Monkey监控您的AWS and GCP accounts有关不安全配置的策略更改和警报。支持OpenStack公共云和私有云。Security Monkey还可以监视和监控您的GitHub组织、团队和存储库

它提供单个UI来浏览和搜索您的所有帐户、地区和云服务。猴子会记住以前的状态,并能准确地告诉你什么时候发生了变化

Security Monkey可以扩展为custom account typescustom watcherscustom auditors,以及custom alerters

它可以在CPython2.7上运行。众所周知,它可以在Ubuntu Linux和OS X上运行

发展分支机构 大师级分支机构

特别注意事项:

Netflix对Security Monkey的支持已经减少,只对小错误进行了修复。也就是说,我们乐于接受并合并修复bug并添加新功能的请求(Pull-Request)

🚨⚠️🥁🎺请阅读:打破1.0的更改🎺🥁⚠️🚨

如果您是第一次升级到1.0,请查看Quickstart以及Autostarting文档,因为Security Monkey有一个新的部署模式。此外,还添加了新的IAM权限

项目资源

实例关系图

组成Security Monkey的组件如下(不是特定于AWS的):

访问图

Security Monkey通过提供的凭据访问帐户以进行扫描(“角色假设”,如果可用)