【问题标题】:Python boto3 exception handlingPython boto3 异常处理
【发布时间】:2020-07-25 07:07:23
【问题描述】:

我想创建一个具有多个属性的 S3 存储桶:

def create_s3_bucket(name):
    s3Client = boto3.client('s3')

    try:
        s3Client.create_bucket(
            Bucket=name,
            ACL="private",
            CreateBucketConfiguration={
                'LocationConstraint': 'eu-central-1',
            },
        )
    except ClientError as e:
        print(e)

    try:
        s3Client.put_public_access_block(
            Bucket=name,
            PublicAccessBlockConfiguration={
                'BlockPublicAcls': True,
                'IgnorePublicAcls': True,
                'BlockPublicPolicy': True,
                'RestrictPublicBuckets': True
            }
        )
    except ClientError as e:
        print(e)

    try:

        s3Client.put_bucket_versioning(
            Bucket=name,
            VersioningConfiguration={
                'Status': 'Enabled'
            }
        )
    except ClientError as e:

        s3Client.put_bucket_tagging(
            Bucket=name,
            Tagging={
                'TagSet': [
                    {
                        'Key': 'firstTag',
                        'Value': 'firstValue'
                    },
                ]
            }
        )
    except ClientError as e:
        print(e)

到目前为止,这有效。 我的问题是:有没有更好、更优雅的方式来处理 ClientError,因为我一直在代码中重复自己? 如果我将每个 API 调用放在一个 try catch 块中,那么可能会出现一个调用没有发生事件的情况。

【问题讨论】:

  • 如果create_bucket 失败,put_public_access_block 将如何工作?你需要充实你的程序在错误情况下的行为。
  • 我需要指定我的问题:如果存储桶例如已经存在然后它抛出一个异常。但这应该可以防止将存储桶属性与后续 API 调用放在一起。
  • 如果你想检查bucket是否存在并完成你的功能,为什么不直接return如果它不存在呢?

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


【解决方案1】:

您可以将所有这些放在同一个 try 块中,这最终会阻止它尝试无法执行的功能(即从未创建存储桶)。

如果引发异常,它应该返回除打印之外的其他内容以识别失败,以防止函数的调用者继续执行逻辑。

此外,在您的异常声明中,如果您的存储桶已创建但无法继续,您可以回滚。

此代码可能如下所示

def create_s3_bucket(name):
    s3Client = boto3.client('s3')

    try:
        s3Client.create_bucket(
            Bucket=name,
            ACL="private",
            CreateBucketConfiguration={
                'LocationConstraint': 'eu-central-1',
            },
        )

        s3Client.put_public_access_block(
            Bucket=name,
            PublicAccessBlockConfiguration={
                'BlockPublicAcls': True,
                'IgnorePublicAcls': True,
                'BlockPublicPolicy': True,
                'RestrictPublicBuckets': True
            }
        )

        s3Client.put_bucket_versioning(
            Bucket=name,
            VersioningConfiguration={
                'Status': 'Enabled'
            }
        )

        s3Client.put_bucket_tagging(
            Bucket=name,
            Tagging={
                'TagSet': [
                    {
                        'Key': 'firstTag',
                        'Value': 'firstValue'
                    },
                ]
            }
        )
    except ClientError as e:
        print(e)
        #Do some tidy up (delete bucket or notify)
        return false

    return true

【讨论】:

  • 好的,这对我有帮助:) 但是我不明白你的代码中返回的目的。我还在文档中看到了这个返回语句,但没有进一步解释。在这种情况下,他们的目的是什么?
  • 很高兴它可以提供一些帮助:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-10-14
  • 1970-01-01
  • 2011-10-09
  • 2017-04-05
  • 1970-01-01
相关资源
最近更新 更多