Data retention policies for Object Storage Service (OSS) use the Write Once Read Many (WORM) feature. This feature stores data in a way that prevents it from being deleted or modified. To prevent anyone, including resource owners, from modifying or deleting objects in an OSS bucket for a specified period, you can set a data retention policy for the bucket. Before the retention period expires, you can only upload objects to and read objects from the bucket. Objects can be modified or deleted only after the retention period ends.
Usage notes
Before you configure retention policies, make sure that you familiarize yourself with this feature. For more information, see Retention policies.
In this topic, the public endpoint of the China (Hangzhou) region is used. If you want to access OSS from other Alibaba Cloud services in the same region as OSS, use an internal endpoint. For more information about OSS regions and endpoints, see Regions and endpoints.
In this topic, an OSSClient instance is created by using an OSS endpoint. If you want to create an OSSClient by using custom domain names or Security Token Service (STS), see Create an OSSClient instance.
Create a data retention policy
The following code shows how to create a data retention policy:
#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;
}Cancel an unlocked data retention policy
The following code shows how to cancel an unlocked data retention policy:
#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;
}Lock a data retention policy
The following code shows how to lock a data retention policy:
#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;
}Get a data retention policy
The following code shows how to retrieve a data retention policy:
#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;
}Extend the object retention period of a locked policy
The following code shows how to extend the retention period of a locked data retention policy:
#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;
}References
For more information about the API operation for creating a data retention policy, see InitiateBucketWorm.
For more information about the API operation for canceling an unlocked data retention policy, see AbortBucketWorm.
For more information about the API operation for locking a data retention policy, see CompleteBucketWorm.
For more information about the API operation for retrieving a data retention policy, see GetBucketWorm.
For more information about the API operation for extending the retention period, see ExtendBucketWorm.