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

设置Bucket版本控制状态

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

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

int main(void)
{
    /* 初始化OSS账号信息。*/
    /* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";

    /* 初始化网络等资源。*/
    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版本控制状态信息

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

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

int main(void)
{
    /* 初始化OSS账号信息。*/
    /* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";

    /* 初始化网络等资源。*/
    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中所有Object的版本信息

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

#include <alibabacloud/oss/OssClient.h>

using namespace AlibabaCloud::OSS;

int main(void)
{
    /* 初始化OSS账号信息。*/
    /* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
    std::string AccessKeyId = "yourAccessKeyId";
    std::string AccessKeySecret = "yourAccessKeySecret";
    /* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
    std::string Endpoint = "yourEndpoint";
    /* 填写Bucket名称,例如examplebucket。*/
    std::string BucketName = "examplebucket";
    /* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
    std::string ObjectName = "exampledir/exampleobject.txt";

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

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

    ListObjectVersionsRequest request(BucketName);
    bool IsTruncated = false;

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

            if (outcome.isSuccess()) {
                /* 查看列出的Object删除标记的各版本信息。*/
                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;
}

相关文档