All Products
Search
Document Center

Object Storage Service:Manage versioning (Python SDK V1)

Last Updated:Nov 28, 2025

Versioning applies to all objects in a bucket. It lets you recover objects to any previous version if they are accidentally overwritten or deleted.

A bucket has three versioning states: unversioned (default), versioning enabled, and versioning suspended. For more information about versioning states, see Versioning.

Usage notes

  • In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.

  • In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials using OSS SDK for Python 1.0.

  • In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient instance by using custom domain names or Security Token Service (STS), see Initialization.

  • To set the versioning state of a bucket, you must have the oss:PutBucketVersioning permission. To retrieve the versioning state of a bucket, you must have the oss:GetBucketVersioning permission. For more information, see Grant custom permissions to a RAM user.

Set the versioning state of a bucket

The following code shows how to set the versioning state of a bucket to Enabled or Suspended.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketVersioningConfig
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Create a bucket versioning configuration.
config = BucketVersioningConfig()
# Set the status to Enabled or Suspended.
config.status = oss2.BUCKET_VERSIONING_ENABLE

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

Get the versioning state of a bucket

The following code shows how to retrieve the versioning state of a bucket.

# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Obtain access credentials from environment variables. Before you run this sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())

# Specify the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# Specify the region where the bucket is located, such as cn-hangzhou. Note: This parameter is required for V4 signatures.
region = "cn-hangzhou"

# Set yourBucketName to the name of the bucket.
bucket = oss2.Bucket(auth, endpoint, "yourBucketName", region=region)

# Get the versioning state of the bucket.
versioning_info = bucket.get_bucket_versioning()
# View the versioning state of the bucket. The state is Enabled or Suspended if versioning was ever enabled. The state is None if versioning was never enabled.
print('bucket versioning status:', versioning_info.status)

References

  • For the complete sample code to manage versioning, see GitHub.

  • For more information about the API operation used to set the versioning state of a bucket, see PutBucketVersioning.

  • For more information about the API operation used to retrieve the versioning state of a bucket, see GetBucketVersioning.