Services >> Cloud >> AWS >> SDK >> Python >> boto3 >> How to manage s3 service

 

 

List buckets and objects in them

#!/usr/bin/python3
import boto3
s3 = boto3.resource('s3')
for bucket in s3.buckets.all():
   print('[-]',bucket.name)
   for item in bucket.objects.all():
       print(" |")
       print(' +--',item.key)

 

 

Delete all object versions in versioned bucket

 

#!/usr/bin/env python 
BUCKET = 'your-bucket-here' 
import boto3 
s3 = boto3.resource('s3')
bucket = s3.Bucket(BUCKET)
bucket.object_versions.delete() 

# if you want to delete the now-empty bucket as well, uncomment this line:
#bucket.delete()