To check if versioning is enabled in an S3 Bucket using Python boto3, we will need to use the get_bucket_versioning()
method of boto3 S3.
Below are 3 ways to code how to get the S3 Bucket versioning status using AWS-SDK for Python, boto3.
- Example 1: Code for checking the versioning status in an S3 Bucket using boto3 Client
- Example 2: Code for checking versioning status in an S3 Bucket using boto3 Resource via Bucket object
- Example 3: Code for checking versioning status in an S3 Bucket using boto3 Resource via Versioning object
The Python scripts below all do the same thing. They check the status of versioning in the target S3 Bucket named radishlogic-bucket.
You can choose whichever method you are comfortable with.
Example 1: Code for checking the versioning status in an S3 Bucket using boto3 Client
import boto3
# Defining a function to get the s3 Bucket Versioning Status
def get_s3_bucket_versioning_status(bucket_name):
# Initialize boto3 S3 Client
s3_client = boto3.client('s3')
# Get the Bucket Versioning
s3_response = s3_client.get_bucket_versioning(Bucket=bucket_name)
# Filter the response to get the Bucket Versioning only
s3_bucket_versioning_status = s3_response.get('Status')
# Return the S3 Bucket Versioning Status
return s3_bucket_versioning_status
# Calling the function to check if S3 Bucket versioning is enabled
s3_bucket_versioning_status = get_s3_bucket_versioning_status(
bucket_name='radishlogic-bucket'
)
# Print the S3 Bucket Versioning status
print(s3_bucket_versioning_status)
When calling get_bucket_versioning()
on an S3 Bucket that has not enabled versioning, the response will have no key of Status
.
Example 2: Code for checking versioning status in an S3 Bucket using boto3 Resource via Bucket object
import boto3
# Defining a function to get the s3 Bucket Versioning Status
def get_s3_bucket_versioning_status(bucket_name):
# Initialize boto3 S3 Resource
s3_resource = boto3.resource('s3')
# Create an S3 Bucket resource
s3_bucket = s3_resource.Bucket(name=bucket_name)
# Get the S3 Bucket versioning resource
s3_bucket_versioning = s3_bucket.Versioning()
# Get the status from the S3 Bucket versioning resource
s3_bucket_versioning_status = s3_bucket_versioning.status
# Return the S3 Bucket Versioning Status
return s3_bucket_versioning_status
# Calling the function to check if S3 Bucket versioning is enabled
s3_bucket_versioning_status = get_s3_bucket_versioning_status(
bucket_name='radishlogic-bucket'
)
# Print the S3 Bucket Versioning status
print(s3_bucket_versioning_status)
When getting the Versioning resource, the boto3 S3 resource will use the get_bucket_versioning()
of boto3 S3 in the background.
Example 3: Code for checking versioning status in an S3 Bucket using boto3 Resource via Versioning object
import boto3
# Defining a function to get the s3 Bucket Versioning Status
def get_s3_bucket_versioning_status(bucket_name):
# Initialize boto3 S3 Resource
s3_resource = boto3.resource('s3')
# Create an S3 Bucket Versioning Resource
s3_bucket_versioning = s3_resource.BucketVersioning(bucket_name=bucket_name)
# Get the status from the S3 Bucket versioning resource
s3_bucket_versioning_status = s3_bucket_versioning.status
# Return the S3 Bucket Versioning Status
return s3_bucket_versioning_status
# Calling the function to check if S3 Bucket versioning is enabled
s3_bucket_versioning_status = get_s3_bucket_versioning_status(
bucket_name='radishlogic-bucket'
)
# Print the S3 Bucket Versioning status
print(s3_bucket_versioning_status)
This is almost the same as Example 2, but with a much shorter code when using boto3 S3 resource.
Outputs
The following are the possible values that the get_s3_bucket_versioning_status() method above.
None
– when the S3 Bucket has not enabled versioning. This is not a string but a Python None
which is equivalent to null
in some programming languages.
Enabled
– When the S3 Bucket has enabled versioning.
Suspended
– When the S3 Bucket has stopped versioning.
Notes
- No need to specify the region of the bucket when checking if object versioning is enabled.
We hope this helps you check the versioning status of your S3 Bucket using Python boto3.
Let us know your experience in the comments below.