【问题标题】:Boto3 InvalidParameterException while executing the lambda functionBoto3 InvalidParameterException 在执行 lambda 函数时
【发布时间】:2021-03-31 02:29:01
【问题描述】:

我在运行 lambda 函数时收到 Boto3 InvalidParameterException。 我正在尝试找到一种方法来处理此异常。

我遇到了以下解决方案:

from boto.exception import BotoServerError

class InvalidParameterException(BotoServerError):
    pass

我正在使用 python3 并了解 boto 现在已弃用,并已被 boto3 取代。 但是我在 boto3 中找不到等效的解决方案。

谁能帮我解决这个问题?

【问题讨论】:

    标签: python-3.x amazon-web-services boto3 boto botocore


    【解决方案1】:

    由于boto 已被弃用,所有modeled 异常都在客户端上可用。您也可以在 API 文档中查找相同的内容,基本上 boto3 的代码是直接从 API 生成的。早期使用 boto 的方法是硬编码的东西并为此编写代码。

    如你所见here

    例如

    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 Example From the docss

    How to handle errors with boto3

    【讨论】:

    • 谢谢!这对我有用。 botocore 库从基类生成几个异常。所以,最好先 Catch 基类,然后根据需要处理异常
    猜你喜欢
    • 2017-03-31
    • 1970-01-01
    • 1970-01-01
    • 2016-10-16
    • 1970-01-01
    • 2017-07-27
    • 2019-05-05
    • 2017-11-22
    • 2017-02-17
    相关资源
    最近更新 更多