版本控制应用于存储空间(Bucket) 内的所有文件(Object)。

通过Object的版本管理,在错误覆盖或者删除Object后,您能够将Bucket中存储的Object恢复至任意时刻的历史版本。Bucket的版本状态包括非版本化(默认)、开启版本控制及暂停版本控制三种。

版本控制的详情请参见开发指南的版本控制介绍

设置Bucket版本控制状态

以下代码用于设置Bucket为开启版本控制(Enabled)或暂停版本控制(Suspended)状态。

using Aliyun.OSS;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// 初始化OssClient。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 设置存储空间版本控制状态为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);
}

设置Bucket版本控制状态的详情请参见PutBucketVersioning

获取Bucket版本控制状态信息

以下代码用于获取Bucket的版本控制状态信息。

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{
    // 获取存储空间版本控制状态信息。
    var result = client.GetBucketVersioning(bucketName);
    Console.WriteLine("Get bucket:{0} Version succeeded ", bucketName);

    Console.WriteLine("bucket version status: {0}", result.Status);
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

获取Bucket版本控制状态的详情请参见GetBucketVersioning

列举Bucket中所有Object的版本信息

以下代码用于列举指定Bucket中包括删除标记(Delete Marker)在内的所有Object的版本信息。

using Aliyun.OSS;
using Aliyun.OSS.Common;
var endpoint = "<yourEndpoint>";
var accessKeyId = "<yourAccessKeyId>";
var accessKeySecret = "<yourAccessKeySecret>";
var bucketName = "<yourBucketName>";
// 创建OssClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
try
{   
    ObjectVersionList result = null;
    var request = new ListObjectVersionsRequest(bucketName);
    do {        
        result = client.ListObjectVersions(request);
        Console.WriteLine("ListObjectVersions succeeded");
        //查看列出的object删除标记的各版本信息。
        foreach (var objectversion in result.ObjectVersionSummaries)
        {
            Console.WriteLine("objectversion key: {0}; objectversion versionid: {1}", objectversion.Key, objectversion.VersionId);
        }
        //查看列出的object各版本信息。
        foreach (var deletemarker in result.DeleteMarkerSummaries)
        {
            Console.WriteLine("deletemarker key: {0}; deletemarker versionid: {1}", deletemarker.Key, deletemarker.VersionId);
        }
        request.KeyMarker = result.NextKeyMarker;
        request.NextVersionIdMarker = result.NextVersionIdMarker ;
    } while (result.IsTruncated)
}
catch (OssException ex)
{
    Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
        ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
}
catch (Exception ex)
{
    Console.WriteLine("Failed with error info: {0}", ex.Message);
}

列举Bucket中包括删除标记(Delete Marker)在内的所有Object的版本信息的详情请参见GetBucketVersions(ListObjectVersions)