All Products
Search
Document Center

Object Storage Service:Manage versioning (C# SDK V2)

Last Updated:Mar 20, 2026

Versioning applies to all objects in a bucket. When versioning is enabled, you can recover any object to any of its previous versions after the object is overwritten or accidentally deleted.

Usage notes

  • The sample code uses China (Hangzhou) (cn-hangzhou) as the example region and connects via the public endpoint by default. To access OSS from other Alibaba Cloud services in the same region, use an internal endpoint instead. For region-to-endpoint mappings, see Regions and endpoints.

  • To set the versioning status of a bucket, you need the oss:PutBucketVersioning permission. To get the versioning status, you need the oss:GetBucketVersioning permission. For details, see Grant a custom access policy to a RAM user.

Sample code

Enable versioning

The following code sets the versioning status of a bucket to Enabled using PutBucketVersioningAsync.

using OSS = AlibabaCloud.OSS.V2;

var region = "cn-hangzhou";
var endpoint = null as string;  // Optional. Overrides the default endpoint when set.
var bucket = "examplebucket";
var status = "Enabled";

// Reads credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

using var client = new OSS.Client(cfg);

var result = await client.PutBucketVersioningAsync(new OSS.Models.PutBucketVersioningRequest()
{
    Bucket = bucket,
    VersioningConfiguration = new OSS.Models.VersioningConfiguration()
    {
        Status = status
    }
});

Console.WriteLine("PutBucketVersioning done");
Console.WriteLine($"StatusCode: {result.StatusCode}");
Console.WriteLine($"RequestId: {result.RequestId}");
Console.WriteLine("Response headers:");
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));

For the complete sample, see PutBucketVersioning.cs.

Get the versioning status

The following code retrieves the current versioning status of a bucket using GetBucketVersioningAsync.

using OSS = AlibabaCloud.OSS.V2;

var region = "cn-hangzhou";
var endpoint = null as string;  // Optional. Overrides the default endpoint when set.
var bucket = "examplebucket";

// Reads credentials from the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables.
var cfg = OSS.Configuration.LoadDefault();
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
cfg.Region = region;
if (endpoint != null)
{
    cfg.Endpoint = endpoint;
}

using var client = new OSS.Client(cfg);

var result = await client.GetBucketVersioningAsync(new OSS.Models.GetBucketVersioningRequest()
{
    Bucket = bucket
});

Console.WriteLine("GetBucketVersioning done");
Console.WriteLine($"StatusCode: {result.StatusCode}");
Console.WriteLine($"RequestId: {result.RequestId}");
Console.WriteLine("Response headers:");
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));
Console.WriteLine($"Status: {result.VersioningConfiguration?.Status}");

For the complete sample, see GetBucketVersioning.cs.