Listing version enabled buckets in s3 using boto3

2.2k views Asked by At

How do I list all s3 bucket names with versioning flag turned on?

I have 100's of s3 buckets. Some of the buckets are having versioning flag turned on. I want to list all those bucket names using boto3.

I have given my python code snippet below.

import boto3
REGION = "us-east-1"

s3client = boto3.client('s3',region_name=REGION)
for bucket in s3client.list_buckets()['Buckets']:
  bucket = bucket['Name']
  response = s3client.get_bucket_versioning(Bucket=bucket)
  if 'Status' in response and response['Status'] == 'Enabled':
    print(bucket)
1

There are 1 answers

2
Fuji Komalan On
import boto3

ACCESS = "AKIAI4JYMHUIYKIFABCD"
SECRET = "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"
REGION = "us-east-1"

s3client = boto3.client('s3',aws_access_key_id=ACCESS,aws_secret_access_key=SECRET,region_name=REGION)
for bucket in s3client.list_buckets()['Buckets']:
  bucket = bucket['Name']
  response = s3client.get_bucket_versioning(Bucket=bucket)
  if 'Status' in response and response['Status'] == 'Enabled':
    print(bucket)