我在运行lambda函数时收到Boto3 InvalidParameterException。我正在想办法处理这个异常。
我遇到了以下解决方案:
from boto.exception import BotoServerError
class InvalidParameterException(BotoServerError):
pass
我使用的是python3,我知道boto现在已经被弃用,取而代之的是boto3。但我在boto3中找不到等效的解决方案。
有人能帮我吗?
由于boto
已弃用,客户端上所有的modeled
异常都可用。您也可以在API文档中查找它,基本上boto3
的代码是直接从API中生成的。boto
的早期方法是硬编码的东西,并为此编写代码。
正如你在这里看到的
例如
import boto3
from botocore.exceptions import ClientError
def get_secret():
secret_name = "MySecretName"
region_name = "us-west-2"
session = boto3.session.Session()
client = session.client(
service_name='secretsmanager',
region_name=region_name,
)
try:
get_secret_value_response = client.get_secret_value(
SecretId=secret_name
)
except ClientError as e:
if e.response['Error']['Code'] == 'ResourceNotFoundException':
print("The requested secret " + secret_name + " was not found")
elif e.response['Error']['Code'] == 'InvalidRequestException':
print("The request was invalid due to:", e)
elif e.response['Error']['Code'] == 'InvalidParameterException':
print("The request had invalid params:", e)
elif e.response['Error']['Code'] == 'DecryptionFailure':
print("The requested secret can't be decrypted using the provided KMS key:", e)
elif e.response['Error']['Code'] == 'InternalServiceError':
print("An error occurred on service side:", e)
AWS Secrets Manager示例来自文档
如何使用boto3 处理错误