Tous les produits
Search
Centre de documentation

Object Storage Service:Retention policies (C++ SDK)

Dernière mise à jour :Aug 18, 2026

Les politiques de conservation des données d'Object Storage Service (OSS) s'appuient sur la fonctionnalité WORM (Write Once Read Many). Cette fonctionnalité stocke les données de manière à empêcher toute suppression ou modification. Pour empêcher toute modification ou suppression des objets d'un bucket OSS, y compris par les propriétaires des ressources, pendant une période donnée, définissez une politique de conservation pour le bucket. Avant l'expiration de cette période, vous pouvez uniquement charger des objets dans le bucket et les lire. La modification ou la suppression des objets n'est possible qu'une fois la période de conservation écoulée.

Notes

  • Avant de configurer les politiques de conservation, assurez-vous de bien comprendre cette fonctionnalité. Pour plus d'informations, consultez Politiques de conservation.

  • Cette rubrique utilise l'endpoint public de la région Chine (Hangzhou). Si vous souhaitez accéder à OSS depuis d'autres services Alibaba Cloud situés dans la même région, utilisez un endpoint interne. Pour plus d'informations sur les régions et les endpoints OSS, consultez Régions et endpoints.

  • Cette rubrique explique comment créer une instance OssClient avec un endpoint OSS. Pour d'autres configurations, telles que l'utilisation d'un domaine personnalisé ou l'authentification via des identifiants du Security Token Service (STS), consultez Créer une instance OssClient.

Créer une politique de conservation des données

Le code suivant montre comment créer une politique de conservation des données :

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

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set Endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set Region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Enter the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";

      /* Initialize resources such as the network. */
      InitializeSdk();

      ClientConfiguration conf;
      conf.signatureVersion = SignatureVersionType::V4;
      /* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
      /* Create a data retention policy and set the object protection period to 1 day. */
      auto outcome = client.InitiateBucketWorm(InitiateBucketWormRequest(BucketName, 1));

      if (outcome.isSuccess()) {      
            std::cout << " InitiateBucketWorm success " << std::endl;
            std::cout << "WormId:" << outcome.result().WormId() << std::endl;
      }
      else {
        /* Handle the exception. */
        std::cout << "InitiateBucketWorm fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
      }

      /* Release resources such as the network. */
      ShutdownSdk();
      return 0;
}

Annuler une politique de conservation des données non verrouillée

Le code suivant montre comment annuler une politique de conservation des données non verrouillée :

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

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set Endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set Region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Enter the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";

      /* Initialize resources such as the network. */
      InitializeSdk();

      ClientConfiguration conf;
      conf.signatureVersion = SignatureVersionType::V4;
      /* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
      /* Cancel the unlocked data retention policy. */
      auto outcome = client.AbortBucketWorm(AbortBucketWormRequest(BucketName));

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

      /* Release resources such as the network. */
      ShutdownSdk();
      return 0;
}

Verrouiller une politique de conservation des données

Le code suivant montre comment verrouiller une politique de conservation des données :

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

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set Endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set Region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Enter the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Enter the data retention policy ID. */
    std::string WormId = "453BA7F15A1C4D599D83EAC0C150****";

      /* Initialize resources such as the network. */
      InitializeSdk();

      ClientConfiguration conf;
      conf.signatureVersion = SignatureVersionType::V4;
      /* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
      /* Lock the data retention policy. */
      auto outcome = client.CompleteBucketWorm(CompleteBucketWormRequest(BucketName, WormId));

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

      /* Release resources such as the network. */
      ShutdownSdk();
      return 0;
}

Obtenir une politique de conservation des données

Le code suivant montre comment récupérer une politique de conservation des données :

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

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set Endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set Region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Enter the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";
     
      /* Initialize resources such as the network. */
      InitializeSdk();

      ClientConfiguration conf;
      conf.signatureVersion = SignatureVersionType::V4;
      /* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
      /* Get the data retention policy. */
      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 {
        /* Handle the exception. */
        std::cout << "GetBucketWorm fail" <<
        ",code:" << outcome.error().Code() <<
        ",message:" << outcome.error().Message() <<
        ",requestId:" << outcome.error().RequestId() << std::endl;
        return -1;
      }

      /* Release resources such as the network. */
      ShutdownSdk();
      return 0;
}

Prolonger la période de conservation des objets d'une politique verrouillée

Le code suivant montre comment prolonger la période de conservation d'une politique de conservation des données verrouillée :

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

int main(void)
{
    /* Initialize the OSS account information. */
            
    /* Set Endpoint to the endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
    std::string Endpoint = "yourEndpoint";
    /* Set Region to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set Region to cn-hangzhou. */
    std::string Region = "yourRegion";
    /* Enter the bucket name. For example, examplebucket. */
    std::string BucketName = "examplebucket";
    /* Enter the data retention policy ID. */
    std::string WormId = "453BA7F15A1C4D599D83EAC0C150****";

      /* Initialize resources such as the network. */
      InitializeSdk();

      ClientConfiguration conf;
      conf.signatureVersion = SignatureVersionType::V4;
      /* Obtain access credentials from environment variables. Before you run this code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set. */
    auto credentialsProvider = std::make_shared<EnvironmentVariableCredentialsProvider>();
    OssClient client(Endpoint, credentialsProvider, conf);
    client.SetRegion(Region);
  
      /* Extend the object retention period of the locked data retention policy. */
      auto outcome = client.ExtendBucketWormWorm(ExtendBucketWormRequest(BucketName, WormId, 20));

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

      /* Release resources such as the network. */
      ShutdownSdk();
      return 0;
}

Références

  • Pour plus d'informations sur l'opération API permettant de créer une politique de conservation des données, consultez InitiateBucketWorm.

  • Pour plus d'informations sur l'opération API permettant d'annuler une politique de conservation des données non verrouillée, consultez AbortBucketWorm.

  • Pour plus d'informations sur l'opération API permettant de verrouiller une politique de conservation des données, consultez CompleteBucketWorm.

  • Pour plus d'informations sur l'opération API permettant de récupérer une politique de conservation des données, consultez GetBucketWorm.

  • Pour plus d'informations sur l'opération API permettant de prolonger la période de conservation, consultez ExtendBucketWorm.