3

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)
mootmoot
  • 12,845
  • 5
  • 47
  • 44
Fuji Komalan
  • 1,979
  • 16
  • 25

1 Answers1

5
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)
Fuji Komalan
  • 1,979
  • 16
  • 25
  • Oh dear, DON'T post code with your AWS API access key. Since you already did this, you need to delete your access key shown. I just move your code up, please delete this post. – mootmoot Dec 21 '17 at 14:47
  • 1
    @mootmoot that's a fake key not a valid one :-). I post that snippet only because, it might be helpful for someone. – Fuji Komalan Dec 21 '17 at 15:10