The versioning state of a bucket applies to all objects in the bucket. Versioning allows you to restore objects in a bucket to previous versions and protects your data from being accidentally overwritten or deleted.

A bucket can be in one of the following versioning states: unversioned (default), versioning-enabled, and versioning-suspended. For more information about versioning, see Overview.

Configure the versioning state of a bucket

The following code provides an example on how to set the versioning state of a bucket to enabled or suspended:
# -*- coding: utf-8 -*-
import oss2
from oss2.models import BucketVersioningConfig
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in Object Storage Service (OSS) is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Set yourBucketName to the name of your bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')

# Initialize the versioning configurations for the bucket. 
config = BucketVersioningConfig()
# Set the versioning state to enabled or suspended. 
config.status = oss2.BUCKET_VERSIONING_ENABLE

# Configure the versioning state of the bucket. 
result = bucket.put_bucket_versioning(config)
# Display the HTTP status code. 
print('http response code:', result.status)

Query the versioning state of a bucket

The following code provides an example on how to query the versioning state of a bucket:
# -*- coding: utf-8 -*-
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Set yourBucketName to the name of your bucket. 
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')

# Query the versioning state of the bucket. 
versioning_info = bucket.get_bucket_versioning()
# Display the versioning state of the bucket. If versioning has been enabled, Enabled or Suspended is returned. If versioning has not been enabled, None is returned. 
print('bucket versioning status:', versioning_info.status)

References

  • For the complete sample code that is used to manage versioning for a bucket, visit GitHub.
  • For more information about the API operation that you can call to configure the versioning state of a bucket, see PutBucketVersioning.
  • For more information about the API operation that you can call to query the versioning state of a bucket, see GetBucketVersioning.