首页 > 解决方案 > 列出我的 s3 中存在的每个存储桶中的对象

问题描述

我的 S3 中有 5 个存储桶。我必须通过 python 脚本为我的 s3 中存在的每个存储桶列出对象。我正在编写这样的脚本:

import boto3

def lambda_handler(event, context):
s3 = boto3.client('s3')
response = s3.list_buckets()

print('Existing buckets:')
for bucket in response['Buckets']:
    for obj in bucket.object.all(['bucket']):
        response = obj.get(
            Key, StorageClass, Size)
        print(response)

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

解决方案


您可以检查以下代码:

import boto3

s3 = boto3.client('s3')
s3r = boto3.resource('s3')

def lambda_handler(event, context):

    response = s3.list_buckets()
    
    for bucket_info in response['Buckets']:
        
        bucket = s3r.Bucket(bucket_info['Name'])
        
        print('Existing buckets:',  bucket_info['Name'])
        
        for object in bucket.objects.all():
            print(' - ', object.key)

推荐阅读