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.