Bucket versioning lets you recover any previous version of an object that was accidentally overwritten or deleted. Once enabled, versioning applies to all objects in the bucket.
A bucket can be in one of three versioning states: disabled (default), enabled, or suspended. For a conceptual overview, see Versioning.
Prerequisites
Before you begin, ensure that you have:
An OSS bucket
The
oss:PutBucketVersioningpermission to set the versioning stateThe
oss:GetBucketVersioningpermission to query the versioning state
For details on granting permissions, see Grant a custom access policy to a RAM user.
Usage notes
The examples in this topic use the public endpoint for the China (Hangzhou) region. To access OSS from other Alibaba Cloud services in the same region, use the internal endpoint instead. For a full list of regions and endpoints, see Regions and endpoints.
Access credentials in the examples are read from environment variables. For setup instructions, see Configure access credentials using OSS SDK for Python 1.0.
The examples create an OSSClient instance using an OSS endpoint. For other initialization options — such as using a custom domain or Security Token Service (STS) credentials — see Initialization.
Enable or suspend versioning
Use put_bucket_versioning to set the versioning state. Set config.status to oss2.BUCKET_VERSIONING_ENABLE to enable versioning, or to oss2.BUCKET_VERSIONING_SUSPEND to suspend it.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketVersioningConfig
# Read access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Replace with the endpoint for your bucket's region.
# Example: https://oss-cn-hangzhou.aliyuncs.com for China (Hangzhou).
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# The region ID is required for the V4 signature algorithm.
region = "cn-hangzhou"
# Replace <yourBucketName> with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "<yourBucketName>", region=region)
# Set the versioning state.
# Use oss2.BUCKET_VERSIONING_ENABLE to enable, or oss2.BUCKET_VERSIONING_SUSPEND to suspend.
config = BucketVersioningConfig()
config.status = oss2.BUCKET_VERSIONING_ENABLE
result = bucket.put_bucket_versioning(config)
# A status of 200 indicates success.
print('HTTP status code:', result.status)For the complete sample code, see bucket_versioning.py on GitHub.
Query the versioning state
get_bucket_versioning returns the current versioning state of the bucket. The status attribute returns Enabled, Suspended, or None if versioning has never been enabled on the bucket.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
# Read access credentials from environment variables.
# Set OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET before running this example.
auth = oss2.ProviderAuthV4(EnvironmentVariableCredentialsProvider())
# Replace with the endpoint for your bucket's region.
endpoint = "https://oss-cn-hangzhou.aliyuncs.com"
# The region ID is required for the V4 signature algorithm.
region = "cn-hangzhou"
# Replace <yourBucketName> with your bucket name.
bucket = oss2.Bucket(auth, endpoint, "<yourBucketName>", region=region)
versioning_info = bucket.get_bucket_versioning()
# Returns "Enabled", "Suspended", or None if versioning has never been enabled.
print('Versioning state:', versioning_info.status)References
API reference: PutBucketVersioning
API reference: GetBucketVersioning