All Products
Search
Document Center

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

Last Updated:Aug 06, 2025

Versioning applies to all files (objects) in a bucket. Object versioning lets you recover an object to any of its previous versions if the object is overwritten or accidentally deleted.

Precautions

  • The sample code in this topic uses China (Hangzhou) with the region ID cn-hangzhou as an example. A public endpoint is used by default. If you want to access OSS from other Alibaba Cloud products in the same region, you must use an internal endpoint. For more information about the mappings between regions and endpoints that OSS supports, see Regions and endpoints.

  • To set the versioning status of a bucket, you must have the oss:PutBucketVersioning permission. To obtain information about the versioning status of a bucket, you must have the oss:GetBucketVersioning permission. For more information, see Grant a custom access policy to a RAM user.

Sample code

Set the versioning status of a bucket

You can use the following code to set the versioning status of a bucket to Enabled.

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou";  // Required. The region where the bucket is located. This example uses the China (Hangzhou) region. Set Region to cn-hangzhou.
var endpoint = null as string;  // Optional. The domain name used to access OSS. This example uses the endpoint of the China (Hangzhou) region. Set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your bucket name";  // Required. The bucket name.
var status = "Enabled";  // Required. The versioning status to be configured for the bucket.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information such as an AccessKey from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly use environment variables to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket for the configuration.
cfg.Region = region;   
// If an endpoint is specified, it overwrites the default endpoint. 
if(endpoint != null) 
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg);

// Call the PutBucketVersioningAsync method to enable versioning for the bucket.
var result = await client.PutBucketVersioningAsync(new OSS.Models.PutBucketVersioningRequest()
{
    Bucket = bucket,
    VersioningConfiguration = new OSS.Models.VersioningConfiguration()
    {
        Status = status
    }
});

// Print the result.
Console.WriteLine("PutBucketVersioning done");  // Indicates that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID. This ID is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // The response headers.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.

Obtain the versioning status of a bucket

You can use the following code to obtain the versioning status of a bucket.

using OSS = AlibabaCloud.OSS.V2;  // Create an alias for the Alibaba Cloud OSS SDK to simplify subsequent use.

var region = "cn-hangzhou";  // Required. The region where the bucket is located. This example uses the China (Hangzhou) region. Set Region to cn-hangzhou.
var endpoint = null as string;  // Optional. The domain name used to access OSS. This example uses the endpoint of the China (Hangzhou) region. Set Endpoint to https://oss-cn-hangzhou.aliyuncs.com.
var bucket = "your Bucket name";  // Required. The bucket name.

// Load the default configurations of the OSS SDK. The configurations automatically read credential information such as an AccessKey from environment variables.
var cfg = OSS.Configuration.LoadDefault();
// Explicitly use environment variables to obtain credentials for identity verification. Format: OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET.
cfg.CredentialsProvider = new OSS.Credentials.EnvironmentVariableCredentialsProvider();
// Set the region of the bucket for the configuration.
cfg.Region = region;   
// If an endpoint is specified, it overwrites the default endpoint. 
if(endpoint != null) 
{
    cfg.Endpoint = endpoint;
} 

// Create an OSS client instance based on the configuration information.
using var client = new OSS.Client(cfg);

// Call the GetBucketVersioningAsync method to obtain the versioning information of the bucket.
var result = await client.GetBucketVersioningAsync(new OSS.Models.GetBucketVersioningRequest()
{
    Bucket = bucket
});

// Print the result.
Console.WriteLine("GetBucketVersioning done");  // Indicates that the operation is complete.
Console.WriteLine($"StatusCode: {result.StatusCode}");  // The HTTP status code.
Console.WriteLine($"RequestId: {result.RequestId}");  // The request ID. This ID is used for troubleshooting in Alibaba Cloud.
Console.WriteLine("Response Headers:");  // The response headers.
result.Headers.ToList().ForEach(x => Console.WriteLine(x.Key + " : " + x.Value));  // Traverse and print all response headers.
Console.WriteLine($"Status: {result.VersioningConfiguration?.Status}");  // Print the versioning information of the bucket.

References