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

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

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

设置Bucket版本控制状态

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

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /*初始化OSS账号信息*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /*初始化网络等资源*/
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /*创建bucket版本配置,状态设置为Enabled或Suspended*/
    SetBucketVersioningRequest setrequest(BucketName, VersioningStatus::Enabled);
    auto outcome = client.SetBucketVersioning(setrequest);

    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "SetBucketVersioning fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /*释放网络等资源*/
    ShutdownSdk();
    return 0;
}

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

获取Bucket版本控制状态信息

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

#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;

int main(void)
{
    /*初始化OSS账号信息*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";

    /*初始化网络等资源*/
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    /*获取bucket版本状态信息*/   
    auto outcome = client.GetBucketVersioning(GetBucketVersioningRequest(BucketName));

    if (!outcome.isSuccess()) {
        /* 异常处理 */
        std::cout << "GetBucketVersioning fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
    }

    /*释放网络等资源*/
    ShutdownSdk();
    return 0;
}

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

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

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

#include <alibabacloud/oss/OssClient.h>

using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息 */
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    std::string Endpoint = "yourEndpoint";
    std::string BucketName = "yourBucketName";
    std::string ObjectName = "yourObjectName";

    /* 初始化网络等资源 */
    InitializeSdk();

    ClientConfiguration conf;
    OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);

    ListObjectVersionsRequest request(BucketName);
    bool IsTruncated = false;

    do {
            auto outcome = client.ListObjectVersions(request);

            if (outcome.isSuccess()) {
                /*查看列出的obejct删除标记的各版本信息*/
                for (auto const &marker : outcome.result().DeleteMarkerSummarys()) {
                   std::cout << "marker key:" << marker.Key() << ",marker versionid:" << marker.VersionId() << std::endl;
                }

                /*查看列出的object各版本信息*/
                for (auto const &obj : outcome.result().ObjectVersionSummarys()) {
                     std::cout << "object key:" << obj.Key() << ",object versionid:" << obj.VersionId() << std::endl;
                }
            }
            else {
                std::cout << "ListObjectVersions fail" <<
                ",code:" << outcome.error().Code() <<
                ",message:" << outcome.error().Message() <<
                ",requestId:" << outcome.error().RequestId() << std::endl;
                break;
            }
            request.setKeyMarker(outcome.result().NextKeyMarker());
            request.setVersionIdMarker(outcome.result().NextVersionIdMarker());
            IsTruncated = outcome.result().IsTruncated();
    } while (IsTruncated);

    /* 释放网络等资源 */
    ShutdownSdk();
    return 0;
}

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