创建策略时出现错误功能”对象没有属性“ put_bucket_policy

时间:2020-07-05 13:08:58

标签: python amazon-web-services amazon-s3 aws-lambda

我正在尝试创建一个公共的存储桶

下面是代码

import json
import boto3

bucket_name = 'some-backet-name'
def s3_client():

    s3 = boto3.client('s3')
    return s3

def create_bucket(bucket_name):

    return s3_client().create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={'LocationConstraint':'eu-central-1'})

def create_bucket_policy():
    bucket_policy = {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AddPerm",
                "Effect": "Allow",
                "Principal": "*",
                "Action": ["s3:*"],
                "Resource": ["arn:aws:s3:::some-backet-name/*"]
            }
        ]
    }
    
    policy_string = json.dumps(bucket_policy)
    
    return s3_client.put_bucket_policy(
        Bucket=bucket_name,
        Policy=policy_string
    )

def lambda_handler(event, context):

    bucket_name = 'some-backet-name'
    #create_bucket(bucket_name)
    create_bucket_policy()

通过https://boto3.amazonaws.com/v1/documentation/api/latest/guide/s3-example-bucket-policies.html

下面是错误

“ errorMessage”:“功能对象没有属性'put_bucket_policy'”,

1 个答案:

答案 0 :(得分:0)

这是因为您没有s3_client变量。实际上,您有一个名为s3_client的函数。我在下面修复了此问题,改为致电s3_client()

还要密切注意针对Python的调整。

import json
import boto3


def s3_client():

    s3 = boto3.client('s3')
    return s3

def create_bucket(bucket_name):

    return s3_client().create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={'LocationConstraint':'eu-central-1'})

def create_bucket_policy():
    bucket_policy = {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AddPerm",
                "Effect": "Allow",
                "Principal": "*",
                "Action": ["s3:*"],
                "Resource": ["arn:aws:s3:::some-backet-name/*"]
            }
        ]
    }
    
    policy_string = json.dumps(bucket_policy)
    
    return s3_client().put_bucket_policy(
        Bucket=BUCKET_NAME,
        Policy=policy_string
    )

def lambda_handler(event, context):

    bucket_name = 'some-backet-name'
    #create_bucket(bucket_name)
    print (create_bucket_policy())

作为改进,您可以将s3_client初始化为全局变量。这样,它将被初始化一次,并且实际上可以在同一主机上存在的Lambda函数的多次调用之间重用。我在下面为您完成了此操作。

import json
import boto3

s3_client = boto3.client('s3')

def create_bucket(bucket_name):

    return s3_client.create_bucket(
        Bucket=bucket_name,
        CreateBucketConfiguration={'LocationConstraint':'eu-central-1'})

def create_bucket_policy():
    bucket_policy = {
        "Version": "2012-10-17",
        "Statement": [
            {
                "Sid": "AddPerm",
                "Effect": "Allow",
                "Principal": "*",
                "Action": ["s3:*"],
                "Resource": ["arn:aws:s3:::some-backet-name/*"]
            }
        ]
    }
    
    policy_string = json.dumps(bucket_policy)
    
    return s3_client.put_bucket_policy(
        Bucket=BUCKET_NAME,
        Policy=policy_string
    )

def lambda_handler(event, context):

    bucket_name = 'some-backet-name'
    #create_bucket(bucket_name)
    print (create_bucket_policy())