To enable object versioning in an S3 Bucket using Python boto3, you can use the put_bucket_versioning()
function. This guide focuses on utilizing this method to control object versions effectively, ensuring data integrity and secure file handling within your AWS S3 environment.
Below are 3 ways to enable versioning in a target S3 Bucket using AWS-SDK for Python, boto3.
- Example 1: Enabling object versioning in an S3 Bucket using boto3 S3 client API
- Example 2: Enabling object versioning in an S3 Bucket using boto3 S3 resource via S3 Bucket
- Example 3: Enabling object versioning in an S3 Bucket using boto3 S3 resource via S3 Versioning
The following examples achieve the same goal: enabling versioning for the AWS S3 Bucket named ‘radishlogic-bucket’.
Feel free to employ any of these methods in your project based on your comfort level.
You can also check if versioning is enabled on your S3 bucket via Python boto3.
Example 1: Code for enabling object versioning in an S3 Bucket using boto3 S3 client API
import boto3
# Define a function to enable S3 Bucket Versioning
def enable_s3_bucket_versioning(bucket_name):
# Initialize boto3 S3 Client
s3_client = boto3.client('s3')
# Enable versioning on S3 Bucket
s3_client.put_bucket_versioning(
Bucket=bucket_name,
VersioningConfiguration={
'Status': 'Enabled'
}
)
# Call the function to enable S3 Bucket versioning
enable_s3_bucket_versioning(bucket_name='radishlogic-bucket')
print('Versioning is now enabled for S3 Bucket')
Example 2: Code for enabling object versioning in an S3 Bucket using boto3 S3 resource via S3 Bucket
import boto3
# Define a function to enable S3 Bucket Versioning
def enable_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()
# Enable object versioning
s3_bucket_versioning.enable()
# Call the function to enable S3 Bucket versioning
enable_s3_bucket_versioning(bucket_name='radishlogic-bucket4')
print('Versioning is now enabled for S3 Bucket')
Example 3: Code for enabling object versioning in an S3 Bucket using boto3 S3 resource via S3 Versioning
import boto3
# Define a function to enable S3 Bucket Versioning
def enable_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)
# Enable object versioning
s3_bucket_versioning.enable()
# Call the function to enable S3 Bucket versioning
enable_s3_bucket_versioning(bucket_name='radishlogic-bucket')
print('Versioning is now enabled for S3 Bucket')
This is almost the same as Example 2. Instead of creating an S3 Bucket object, it directly creates the S3 Versioning object of the S3 Bucket before enabling it.
Conclusion
We covered three methods of boto3 to enable object versioning: the Client API, using the Bucket resource, and direct bucket versioning. Each offers a different way to handle S3 bucket versioning.
With these methods, you gain control over versioning, enhancing file management and security within your Amazon S3 storage.
Feel free to drop your questions and comments below.