boto3 - 如何从 boto 导入特定错误?

标签 boto3

运行代码时遇到此异常(打印类以进行调试):

An error occurred (DBInstanceNotFound) when calling the DescribeDBInstances operation: DBInstance <instance name> not found.
<class 'botocore.errorfactory.DBInstanceNotFoundFault'>

我想像这样正确处理异常:

from botocore.exceptions import DBInstanceNotFoundFault
from botocore.errorfactory import DBInstanceNotFoundFault
try:
    <fetch info about db instance>
except DBInstanceNotFoundFault as e:
    <handle error>

不幸的是,DBInstanceNotFoundFault既不存在于botocore.exceptions中,也不存在于botocore.errorfactory中。 我克隆了boto3以及botocoregrep'd DBInstanceNotFoundFault,但就是找不到它。

如何导入?

最佳答案

对于 boto3 异常,您实际上并不导入错误 - 而是通过客户端访问它。看看这个:https://github.com/boto/boto3/issues/1195#issuecomment-317108970

import boto3
rds = boto3.client('rds')
try:
    ...
except rds.exceptions.DBInstanceNotFoundFault:
    ...

关于boto3 - 如何从 boto 导入特定错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48086787/

相关文章:

python - 使用 boto3 dynamodb 客户端时出现 FilterExpression 语法错误

amazon-web-services - 尝试创建或更新 Route53 A 记录时出现 InvalidInput 错误

mocking - 如何模拟 boto3 的 SES 版本 2 API?

python - 使用 boto3 在 S3 存储桶中移动对象的最快方法

python - 使用具有 IAM 角色的 boto3 连接到 Amazon S3

python-3.x - 使用预签名 URL 从 Amazon S3 读取 zip 文件,无需知道对象键和存储桶名称

python - 如何通过 Mturk API 从沙盒上的 HIT 获取结果

python-3.x - 从 AWS S3 存储桶读取缓慢

python Boto3 S3 : List only current directory file ignoring subdirectory files

amazon-web-services - AWS Lambda 控制台 - 升级 boto3 版本