Todos os produtos
Search
Central de documentação

Object Storage Service:Gerenciar versionamento (C# SDK V2)

Última atualização: Sep 22, 2026

O versionamento se aplica a todos os arquivos (objetos) em um bucket. Com o versionamento de objetos, você pode recuperar qualquer versão anterior de um objeto caso ele seja sobrescrito ou excluído acidentalmente.

Precauções

  • O código de exemplo neste tópico utiliza China (Hangzhou) com o ID de região cn-hangzhou como exemplo. Por padrão, um endpoint público é usado. Para acessar o OSS a partir de outros produtos Alibaba Cloud na mesma região, utilize um endpoint interno. Para mais informações sobre os mapeamentos entre regiões e endpoints compatíveis com o OSS, consulte Regions and endpoints.

  • Para definir o status de versionamento de um bucket, você precisa ter a permissão oss:PutBucketVersioning. Para obter informações sobre o status de versionamento de um bucket, você precisa ter a permissão oss:GetBucketVersioning. Para mais informações, consulte Grant a custom access policy to a RAM user.

Código de exemplo

Definir o status de versionamento de um bucket

Use o código a seguir para definir o status de versionamento de um bucket como 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.

Obter o status de versionamento de um bucket

Use o código a seguir para obter o status de versionamento de um 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.

Referências

  • Para obter o código de exemplo completo sobre como definir o status de versionamento de um bucket, consulte PutBucketVersioning.cs.

  • Para obter o código de exemplo completo sobre como consultar o status de versionamento de um bucket, acesse GetBucketVersioning.cs.