Skip to content

Instantly share code, notes, and snippets.

@jicowan
Created July 14, 2017 05:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jicowan/29868780e3c78ba7d48e1d501e58ef3f to your computer and use it in GitHub Desktop.
Save jicowan/29868780e3c78ba7d48e1d501e58ef3f to your computer and use it in GitHub Desktop.
enable mfa-delete on a bucket
import boto3
from botocore.exceptions import ClientError
s3_client = boto3.client('s3')
s3_bucket = boto3.resource('s3')
bucket_name = raw_input('Enter the name of the bucket that you want to enable MFA-delete on: ')
mfa_token = raw_input('Enter your MFA serial number and token code, e.g. <deviceSerialNumber> <tokenCode>: ')
try:
s3_bucket.meta.client.head_bucket(Bucket=bucket_name)
except ClientError as e:
if int(e.response['Error']['Code']) == 404:
print 'Bucket does not exist'
exit()
try:
s3_client.put_bucket_versioning(Bucket = bucket_name,
MFA = mfa_token,
VersioningConfiguration = {
'MFADelete' : 'Enabled',
'Status' : 'Enabled'
}
)
except ClientError as e:
print e
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment