To stop versioning in an S3 Bucket, we can use the boto3 method put_bucket_versioning()
with the VersioningConfiguration
parameter to be set to a Status
of Suspended
.
Below are 3 methods on how we can stop versioning in an S3 Bucket using AWS-SDK for Python, boto3.
- Example 1: Disable file versioning in an S3 Bucket using boto3 S3 client
- Example 2: Disable file versioning in an S3 Bucket using boto3 S3 resource via Bucket object
- Example 3: Disable file versioning in an S3 Bucket using boto3 S3 resource via S3 Versioning object
The Python scripts below do the same thing: suspend the versioning of the target S3 Bucket named ‘radishlogic-bucket’.
You may use any method that you like depending on which you are comfortable using.
Interestingly, suspending versioning uses the same boto3 method as enabling versioning, put_bucket_versioning(). The only difference is that the ‘Status’ is ‘Suspended’ instead of ‘Enabled’.
Example 1: Code for stopping file versioning in an S3 Bucket using boto3 S3 client
import boto3
# Define a function to disable S3 Bucket Versioning
def disable_s3_bucket_versioning(bucket_name):
# Initialize boto3 S3 Client
s3_client = boto3.client('s3')
# Disable/Suspend versioning on S3 Bucket
s3_client.put_bucket_versioning(
Bucket=bucket_name,
VersioningConfiguration={
'Status': 'Suspended'
}
)
# Call the function to disable S3 Bucket versioning
disable_s3_bucket_versioning(bucket_name='radishlogic-bucket3')
print('Versioning is now disabled for S3 Bucket')
Example 2: Code for stopping file versioning in an S3 Bucket using boto3 S3 resource via Bucket object
import boto3
# Define a function to disable S3 Bucket Versioning
def disable_s3_bucket_versioning(bucket_name):
# Initialize boto3 S3 resource
s3_resource = boto3.resource('s3')
# Create S3 Bucket resource object
s3_bucket = s3_resource.Bucket(name=bucket_name)
# Get the Versioning resource object of the S3 Bucket
s3_bucket_versioning = s3_bucket.Versioning()
# Disable/Suspend object versioning
s3_bucket_versioning.suspend()
# Call the function to disable S3 Bucket versioning
disable_s3_bucket_versioning(bucket_name='radishlogic-bucket4')
print('Versioning is now disabled for S3 Bucket')
Example 3: Code for stopping file versioning in an S3 Bucket using boto3 S3 resource via S3 Versioning object
import boto3
# Define a function to enable S3 Bucket Versioning
def disable_s3_bucket_versioning(bucket_name):
# Initialize boto3 S3 resource
s3_resource = boto3.resource('s3')
# Create an Versioning resource object of the S3 Bucket
s3_bucket_versioning = s3_resource.BucketVersioning(bucket_name=bucket_name)
# Disabled/Suspend object versioning
s3_bucket_versioning.suspend()
# Call the function to disable S3 Bucket versioning
disable_s3_bucket_versioning(bucket_name='radishlogic-bucket5')
print('Versioning is now disabled for S3 Bucket')
This is almost the same as Example 2, but uses the Versioning object directly making a shorter code.
Disabled vs Suspended S3 Bucket Versioning
The term used by AWS for stopping S3 bucket versioning by using “Suspend” instead of “Disable” is primarily due to the nature of the action.
When you ‘suspend’ versioning in an S3 bucket, it doesn’t permanently disable or delete the existing versions of objects within the bucket. Instead, it halts the creation of new versions while retaining the existing versions. This provides an opportunity to re-enable versioning in the future without losing the historical versions that were previously stored.
By using the term ‘Suspend’, AWS indicates that the action temporarily pauses or halts the ongoing process of versioning without terminating it entirely. The word choice aligns with the reversible nature of the action, allowing users to toggle versioning back on later without losing the historical versions already present in the bucket.
AWS aims for clarity in its terminology, ensuring users understand that suspending versioning is a temporary action that can be reversed, preserving the existing version history while preventing the creation of new versions.
Checking
You may also check the updated status of S3 object versioning for your target bucket via the boto3 using the get_bucket_versioning()
method of boto3.
We hope that this helps you with stopping object file versioning in your S3 Bucket using Python boto3.
Feel free to write your experience or questions in the comments below.