Listing objects from each and every bucket present in my s3

I have 5 buckets in my S3. I have to list objects for every bucket present in my s3 by python script. I am writing script something like this :

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)

Upvotes: 0

Views: 136

Answers (1)

Marcin
Marcin

Reputation: 238199

You can check the following code:

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)

Upvotes: 1

Related Questions