首页 > 解决方案 > AWS Lambda Python 获取最新的 S3 存储桶

问题描述

我正在编写一个将策略附加到最新存储桶的 Lambda 函数。该脚本将策略附加到命名存储桶,但我不太确定如何获取最新创建的存储桶的名称。任何想法表示赞赏:)。当前代码如下。

import boto3
import json

client = boto3.client('s3')
bucket_name = '*'
bucket_policy = {
            "Version": "2012-10-17",
            "Statement": [{
                "Sid": "DenyS3PublicObjectACL",
                "Effect": "Deny",
                "Principal": "*",
                "Action": ["s3:PutObjectAcl"],
                "Resource": "arn:aws:s3:::%s/*" #% bucket_name,
                "Condition": {
                    "StringEqualsIgnoreCaseIfExists": {
                        "s3:x-amz-acl": [
                            "public-read",
                            "public-read-write",
                            "authenticated-read"
                            ]
                        }
                    }
                }]
            }


bucket_policy = json.dumps(bucket_policy) # Converting the policy into a JSON string.

def lambda_handler(event, context):
    response = client.put_bucket_policy(Bucket=bucket_name, ConfirmRemoveSelfBucketAccess=True, Policy=bucket_policy)

标签: pythonamazon-web-servicesamazon-s3aws-lambdabucket

解决方案


推荐阅读