Object Storage Service:Enable versioning for data recovery and version management
Last Updated:May 27, 2025
Versioning is a bucket-level data protection feature. When you overwrite or delete objects stored in a versioning-enabled bucket, Object Storage Service (OSS) saves the objects as previous versions in the bucket. After you enable versioning for a bucket, you can recover objects in the bucket to a previous version to protect your data from being accidentally overwritten or deleted.
Scenarios
To ensure data security, we recommend that you enable versioning in the following scenarios:
Recover deleted data
OSS does not provide the recycle bin feature. You can configure versioning to recover deleted data.
Recover overwritten data
Numerous temporary versions are created in scenarios where modifications are frequently made, such as online collaborative documents and documents stored in online storage. You can use the versioning feature to retrieve a specified version of an object stored in the bucket.
Usage notes
Billing
If you enable versioning for a bucket, you are charged for the storage of all versions of objects in the bucket. To reduce storage costs, we recommend that you configure bucket lifecycle rules to delete previous versions that you no longer need. For more information, see Lifecycle. When you download a previous version of an object or recover an object to a previous version, you are charged request and traffic fees. For more information, see Billing overview.
Required permissions
Only the bucket owner or RAM users who have the PutBucketVersioning permission can configure versioning for a bucket.
Feature conflicts
A bucket cannot have versioning and retention policies configured at the same time.
If versioning is enabled for the bucket, the request header x-oss-forbid-overwrite that is attached when uploading files to prevent overwriting files with the same name will not take effect. For more information, see Request headers.
OSS-HDFS in versioned buckets
Do not enable OSS-HDFS and versioning for a bucket at the same time.
Versioning states
A bucket can be in one of the following versioning states: disabled, enabled, and suspended.
By default, the versioning state of a bucket is disabled. After versioning is enabled for a bucket, the versioning state of the bucket cannot be rolled back to disabled. However, you can suspend versioning for a bucket for which versioning is enabled.
When the bucket versioning is in the "Enabled" state, OSS generates a globally unique random string as the version ID for newly uploaded objects. For more information about operations on objects in a versioning-enabled bucket, see Manage objects in a versioning-enabled bucket.
When an object is uploaded to a bucket for which versioning is suspended, OSS generates the "null" string as the version ID of the object. For more information about operations on objects in a versioning-suspended bucket, see Manage objects in a versioning-suspended bucket.
Note
In a versioning-enabled bucket, all versions of an object are stored. These versions consume storage space and incur storage fees. We recommend that you configure lifecycle rules based on your usage scenarios to convert current or previous versions of objects to Infrequent Access or Archive storage classes and delete previous versions that are no longer needed. This reduces your storage costs. For more information, see Configure lifecycle rules based on the last modified time for versioning-enabled buckets to reduce storage costs.
Data protection
The following table describes how OSS processes deleted and overwritten data in buckets in different versioning states to help you understand the data protection mechanism of versioning.
Versioning state
Object overwriting
Delete Object
Disabled
The existing object is overwritten and cannot be recovered. Only the current object version can be accessed.
The object is deleted and cannot be accessed.
Enabled
A new version with a unique ID is generated for the object. The existing object is stored as a previous version.
A delete marker with a globally unique version ID is added to the object as the current version. The existing object is stored as a previous version.
Suspend
A new version with the version ID null is generated for the object.
If the object already has a previous version or delete marker whose version ID is null, the previous version or delete marker is overwritten by the new null version. Other previous versions or delete markers whose version IDs are not null are not affected.
A delete marker with the version ID null is added to the object.
If the object already has a previous version or delete marker whose version ID is null, the previous version or delete marker is overwritten by the new delete marker. Other previous versions or delete markers whose version IDs are not null are not affected.
The following figures show how OSS processes data when an object with the same name as an existing object is uploaded to or an object is deleted from a bucket for which versioning is enabled or suspended. For readability, all version IDs in the figures are in the simple format.
Overwrite an object in a versioning-enabled bucket
When you upload an object repeatedly to a versioning-enabled bucket, the object is overwritten in each upload. A version with a unique version ID is generated for the object each time the object is overwritten.
Delete an object from a versioning-enabled bucket
When you delete an object from a versioning-enabled bucket, OSS adds a delete marker to the object as the current version of the object instead of permanently deleting this object. The previous versions of the object are not deleted. If you upload an object with the same name after the delete marker is added, a new version with a unique version ID is added as the current version.
Overwrite an object in a versioning-suspended bucket
When you upload an object with the same name as an existing object in a bucket for which versioning is suspended, a new version whose version ID is null is added to the bucket and the previous versions of the object are retained. If you upload another object with the same name to the bucket again, a new version whose version ID is null overwrites the current version whose version ID is null.
Delete an object from a versioning-suspended bucket
When you delete an object from a bucket for which versioning is suspended, OSS adds a delete marker to the object as the current version of the object instead of permanently deleting this object. The previous versions of the object are not deleted.
In summary, deleted and overwritten data is stored as previous versions in a versioning-enabled or versioning-suspended bucket. After you enable versioning for a bucket, you can restore objects in the bucket to a previous version to protect your data from being accidentally overwritten or deleted.
Enable versioning
Use the OSS console
When versioning is enabled for a bucket, OSS specifies a unique ID for each version of an object stored in the bucket.
On the Create Bucket page, configure the parameters.
In the Versioning section, select Enable. For more information about how to configure other parameters, see Create buckets.
Click OK.
Enable versioning for an existing bucket
In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.
In the left-side navigation pane, choose Content Security > Versioning.
On the Versioning page, click Enable.
In the dialog box that appears, click OK.
ObjectsPrevious VersionsShowPrevious VersionsHideDecreased response speedAfter you enable versioning, to view all versions of objects in the bucket, click Show to the right of Historical Versions on the Files page. To view only the current versions of objects, click Hide to the right of Historical Versions. Hiding previous versions of objects does not accelerate object listing performance. If object listing is slow, see Decreased response speed to resolve the issue.
Use OSS SDKs
SDK overviewThe following sample code provides examples on how to enable versioning for a bucket by using OSS SDKs for common programming languages. For more information about how to enable versioning for a bucket by using OSS SDKs for other programming languages, see SDK overview.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the region in which the bucket is located. Example: cn-hangzhou.
String region = "cn-hangzhou";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Create an OSSClient instance.
// When the OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// Explicitly declare the use of V4 signature algorithm
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Enable versioning.
BucketVersioningConfiguration configuration = new BucketVersioningConfiguration();
configuration.setStatus(BucketVersioningConfiguration.ENABLED);
SetBucketVersioningRequest request = new SetBucketVersioningRequest(bucketName, configuration);
ossClient.setBucketVersioning(request);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket= "yourBucketName";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
try {
// Enable versioning.
$ossClient->putBucketVersioning($bucket, "Enabled");
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
The secret generated when creating the application.
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketVersioningConfig
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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.
# Replace yourBucketName with the name of your bucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
# Initialize versioning configurations for the bucket.
config = BucketVersioningConfig()
# Enable versioning.
config.status = oss2.BUCKET_VERSIONING_ENABLE
result = bucket.put_bucket_versioning(config)
# Show the HTTP status code.
print('http response code:', result.status)
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 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.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou.
const string region = "cn-hangzhou";
// Create a ClientConfiguration instance and modify parameters as required.
var conf = new ClientConfiguration();
// Use the signature algorithm V4.
conf.SignatureVersion = SignatureVersion.V4;
// Create an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret, conf);
client.SetRegion(region);
try
{
// Set the versioning status of the bucket to Enabled.
client.SetBucketVersioning(new SetBucketVersioningRequest(bucketName, VersioningStatus.Enabled));
Console.WriteLine("Create bucket Version succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Create bucket Version failed. {0}", ex.Message);
}
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to 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. Specify the actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a bucket.
// Set yourBucketName to the name of the bucket.
err = client.CreateBucket("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Set the versioning state of the bucket to Enabled.
config := oss.VersioningConfig{Status: "Enabled"}
err = client.SetBucketVersioning("yourBucketName", config)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
#include
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS.*/
/* Set yourEndpoint to 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.*/
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket.*/
std::string BucketName = "examplebucket";
/* Initialize resources such as networks.*/
InitializeSdk();
ClientConfiguration conf;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.*/
auto credentialsProvider = std::make_shared();
OssClient client(Endpoint, credentialsProvider, conf);
/* Set the versioning state to Enabled.*/
SetBucketVersioningRequest setrequest(BucketName, VersioningStatus::Enabled);
auto outcome = client.SetBucketVersioning(setrequest);
if (!outcome.isSuccess()) {
/* Handle exceptions.*/
std::cout << "SetBucketVersioning fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources.*/
ShutdownSdk();
return 0;
}
Use ossutil
You can use the command-line tool ossutil to set the versioning state of a specified bucket. For information about how to install ossutil, see Install ossutil.
The following command is used to enable versioning for a specified bucket:
ossutil api put-bucket-versioning --bucket examplebucket --versioning-configuration "{\"Status\":\"Enabled\"}"
You can suspend versioning for a versioned bucket to stop OSS from generating new versions for objects. If a new version is generated for an object in a versioning-suspended bucket, OSS sets the ID of the new version to null and retains the previous versions of the object.
To suspend versioning for a bucket, perform the following steps:
In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.
In the left-side navigation pane, choose Content Security > Versioning.
On the Versioning page, click Suspend.
In the message that appears, click OK.
Use OSS SDKs
The following sample code provides examples on how to enable versioning for a bucket by using OSS SDKs for common programming languages. For more information about how to enable versioning for a bucket by using OSS SDKs for other programming languages, see SDK overview.
import com.aliyun.oss.*;
import com.aliyun.oss.common.auth.*;
import com.aliyun.oss.common.comm.SignVersion;
import com.aliyun.oss.model.*;
public class Demo {
public static void main(String[] args) throws Exception {
// Specify the endpoint of the region. In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// Specify the region in which the bucket is located. Example: cn-hangzhou.
String region = "cn-hangzhou";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
EnvironmentVariableCredentialsProvider credentialsProvider = CredentialsProviderFactory.newEnvironmentVariableCredentialsProvider();
// Specify the name of the bucket. Example: examplebucket.
String bucketName = "examplebucket";
// Create an OSSClient instance.
// When the OSSClient instance is no longer used, call the shutdown method to release resources.
ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration();
// Explicitly declare the use of V4 signature algorithm
clientBuilderConfiguration.setSignatureVersion(SignVersion.V4);
OSS ossClient = OSSClientBuilder.create()
.endpoint(endpoint)
.credentialsProvider(credentialsProvider)
.clientConfiguration(clientBuilderConfiguration)
.region(region)
.build();
try {
// Suspend versioning.
BucketVersioningConfiguration configuration = new BucketVersioningConfiguration();
configuration.setStatus(BucketVersioningConfiguration.SUSPENDED);
SetBucketVersioningRequest request = new SetBucketVersioningRequest(bucketName, configuration);
ossClient.setBucketVersioning(request);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
}
}
}
}
<?php
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
}
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
}
use OSS\OssClient;
use OSS\Core\OssException;
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
$accessKeyId = getenv("OSS_ACCESS_KEY_ID");
$accessKeySecret = getenv("OSS_ACCESS_KEY_SECRET");
// In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint.
$endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
$bucket= "yourBucketName";
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint);
try {
// Suspend versioning.
$ossClient->putBucketVersioning($bucket, "Suspended");
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
}
print(__FUNCTION__ . ": OK" . "\n");
const OSS = require("ali-oss");
const client = new OSS({
// Set yourregion to the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou.
region: "oss-cn-hangzhou",
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
accessKeyId: process.env.OSS_ACCESS_KEY_ID,
accessKeySecret: process.env.OSS_ACCESS_KEY_SECRET,
// Specify the bucket name. Example: examplebucket.
bucket: "examplebucket",
});
async function putBucketVersioning() {
// Suspend versioning.
const status = "Suspended";
const result = await client.putBucketVersioning("examplebucket", status);
console.log(result);
}
putBucketVersioning();
# -*- coding: utf-8 -*-
import oss2
from oss2.credentials import EnvironmentVariableCredentialsProvider
from oss2.models import BucketVersioningConfig
# Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
auth = oss2.ProviderAuth(EnvironmentVariableCredentialsProvider())
# 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.
# Replace yourBucketName with the name of your bucket.
bucket = oss2.Bucket(auth, 'https://oss-cn-hangzhou.aliyuncs.com', 'yourBucketName')
# Initialize versioning configurations for the bucket.
config = BucketVersioningConfig()
# Suspend versioning.
config.status = oss2.BUCKET_VERSIONING_SUSPENDED
result = bucket.put_bucket_versioning(config)
# Show the HTTP status code.
print('http response code:', result.status)
using Aliyun.OSS;
// 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.
var endpoint = "yourEndpoint";
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
var accessKeyId = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_ID");
var accessKeySecret = Environment.GetEnvironmentVariable("OSS_ACCESS_KEY_SECRET");
// Specify the name of the bucket. Example: examplebucket.
var bucketName = "examplebucket";
// Initialize an OSSClient instance.
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
// Set the versioning state of the bucket to Suspended.
client.SetBucketVersioning(new SetBucketVersioningRequest(bucketName, VersioningStatus.Suspended));
Console.WriteLine("Create bucket Version succeeded");
}
catch (Exception ex)
{
Console.WriteLine("Create bucket Version failed. {0}", ex.Message);
}
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
/// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to 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. Specify the actual endpoint.
client, err := oss.New("yourEndpoint", "", "", oss.SetCredentialsProvider(&provider))
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create a bucket.
// Set yourBucketName to the name of the bucket.
err = client.CreateBucket("yourBucketName")
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Suspend versioning.
config := oss.VersioningConfig{Status: "Suspended"}
err = client.SetBucketVersioning("yourBucketName", config)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
#include
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize information about the account that is used to access OSS.*/
/* Set yourEndpoint to 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.*/
std::string Endpoint = "yourEndpoint";
/* Specify the name of the bucket. Example: examplebucket.*/
std::string BucketName = "examplebucket";
/* Initialize resources such as networks.*/
InitializeSdk();
ClientConfiguration conf;
/* Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are configured.*/
auto credentialsProvider = std::make_shared();
OssClient client(Endpoint, credentialsProvider, conf);
/* Set the versioning state of the bucket to Suspended.*/
SetBucketVersioningRequest setrequest(BucketName, VersioningStatus::Suspended);
auto outcome = client.SetBucketVersioning(setrequest);
if (!outcome.isSuccess()) {
/* Handle exceptions.*/
std::cout << "SetBucketVersioning fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release resources such as network resources.*/
ShutdownSdk();
return 0;
}
Use ossutil
You can use the command-line tool ossutil to set the versioning state of a specified bucket. For information about how to install ossutil, see Install ossutil.
The following command is used to suspend versioning for a specified bucket:
ossutil api put-bucket-versioning --bucket examplebucket --versioning-configuration "{\"Status\":\"Suspended\"}"
When you delete an object from a versioning-enabled bucket, OSS adds a delete marker to the object as the current version of the object instead of permanently deleting this object. The previous versions of the object are not deleted. You can recover a previous version of an object in the OSS console.
In the left-side navigation pane, click Buckets. On the Buckets page, find and click the desired bucket.
Choose Object Management > Objects.
Click Previous Versions and then click Show on the right.
Select the previous version that you want to recover and click Restore.
In the dialog box that appears, click OK.
Use ossutil
You can use the command-line tool ossutil to recover historical versions of deleted files. For information about installing ossutil, see Install ossutil.
The following command is used to restore the example.txt object in the bucket examplebucket to the state of version number 123.
The above operations are implemented based on APIs. If your program requires a high level of customization, you can directly call RESTful API requests. To directly call a RESTful API request, you must include the signature calculation in your code. For more information, see PutBucketVersioning.