Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 在Lambda中使用Boto3迭代s3存储桶_Python 3.x_Aws Lambda_Boto3 - Fatal编程技术网

Python 3.x 在Lambda中使用Boto3迭代s3存储桶

Python 3.x 在Lambda中使用Boto3迭代s3存储桶,python-3.x,aws-lambda,boto3,Python 3.x,Aws Lambda,Boto3,我正在尝试迭代特定帐户中的所有bucket,并将所有bucket名称添加到列表中,以便将bucket策略和日志记录应用到存储在列表中的那些bucket 这是我的密码: import json import boto3 def lambda_handler(event, context): # TODO implement # print("hanlder:event") # print(event) # bucketDump() s

我正在尝试迭代特定帐户中的所有bucket,并将所有bucket名称添加到列表中,以便将bucket策略和日志记录应用到存储在列表中的那些bucket

这是我的密码:

import json
import boto3

def lambda_handler(event, context):
    # TODO implement

    # print("hanlder:event")
    # print(event)
    # bucketDump()
    setBucketPolicy(bucketDump())


def bucketDump():
    ##This program lists all exsisting buckets within an aws account (Tommy's Personal Account)
    s3 = boto3.client('s3')
    response = s3.list_buckets()

    # Output the bucket names
    # print('Existing buckets:')

    for bucket in response['Buckets']:
        if bucket is not None:
            buckets = []
            value = bucket["Name"]
            buckets.append(value)
           
        else: 
            break
    return buckets
            

    ##setting a bucket policy
def setBucketPolicy(buckets):
    print(buckets)

我的问题是它只返回最后一个bucket,所以当我调用函数时,它只打印一个bucket。但是,如果我去掉“return”并只打印
print(bucket['Name'])
我会得到一个所有bucket的打印列表,但显然这在函数中是不可用的。提前感谢您的帮助

我需要在for循环之外声明bucket=[]