对象存储OSS允许针对存储空间(Bucket)设置基于时间的合规保留策略,保护周期为1天到70年。本文介绍如何新建、获取、锁定合规保留策略等。

背景信息

OSS支持WORM(Write Once Read Many)特性,允许您以不可删除、不可篡改的方式保存和使用数据。

当基于时间的合规保留策略创建24小时后未提交锁定,则该策略自动失效。当合规保留策略锁定后,您可以在Bucket中上传和读取文件(Object),但是在Object的保留时间到期之前,不允许删除Object及合规保留策略,且无法缩短策略保护周期,仅允许延长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";

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

      ClientConfiguration conf;
      OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
        /*创建合规保留策略,指定Object保护天数为10天*/
      auto outcome = client.InitiateBucketWorm(InitiateBucketWormRequest(BucketName, 10));

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

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

有关新建合规保留策略的详情,请参见InitiateBucketWorm

取消未锁定的合规保留策略

以下代码用于取消未锁定的合规保留策略:

#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);
  
      /*取消未锁定的合规保留策略*/
      auto outcome = client.AbortBucketWorm(AbortBucketWormRequest(BucketName));

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

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

有关取消未锁定的合规保留策略的详情,请参见AbortBucketWorm

锁定合规保留策略

以下代码用于锁定合规保留策略:

#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 WormId = "yourWormId";

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

      ClientConfiguration conf;
      OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
        /*锁定合规保留策略*/
      auto outcome = client.CompleteBucketWorm(CompleteBucketWormRequest(BucketName, WormId));

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

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

有关锁定合规保留策略的详情,请参见CompleteBucketWorm

获取合规保留策略

以下代码用于获取合规保留策略:

#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);
  
        /*获取合规保留策略*/
      auto outcome = client.GetBucketWorm(GetBucketWormRequest(BucketName));

      if (outcome.isSuccess()) {      
            std::cout << " GetBucketWorm success " << std::endl;
            std::cout << " CreationDate:" << outcome.result().CreationDate() <<
            ",State:" << outcome.result().State() <<
            ",WormId:" << outcome.result().WormId() << std::endl;
      }
      else {
        /*异常处理*/
        std::cout << "GetBucketWorm fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        ShutdownSdk();
        return -1;
      }

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

有关获取合规保留策略的详情,请参见GetBucketWorm

延长Object的保留天数

以下代码用于延长已锁定的合规保留策略中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 WormId = "yourWormId";

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

      ClientConfiguration conf;
      OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
  
        /*延长已锁定的合规保留策略中Object的保留天数*/
      auto outcome = client.ExtendBucketWormWorm(ExtendBucketWormRequest(BucketName, WormId, 20));

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

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

有关延长已锁定的合规保留策略中Object的保留天数详情,请参见ExtendBucketWorm