OSS'e yüklenen tüm verilere sık sık erişilmesi gerekmez. Veri uyumluluğu veya arşivleme amaçları doğrultusunda, bazı verilerin soğuk depolama sınıfında saklanması gerekebilir. Diğer senaryolarda ise bir pakette artık ihtiyaç duyulmayan verileri toplu olarak silmek isteyebilirsiniz. Son değiştirilme zamanına dayalı yaşam döngüsü kuralları yapılandırabilirsiniz. Bu kurallar, nesneleri düzenli aralıklarla daha soğuk depolama sınıflarına geçirir veya nesneleri silerek depolama maliyetlerini düşürür.
Notlar
Nesnelerin son değiştirilme zamanına dayalı yaşam döngüsü kuralları yapılandırmadan önce bu özelliği iyice anlayın. Daha fazla bilgi için bkz. Son değiştirilme zamanına dayalı yaşam döngüsü kuralları.
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmaktadır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e erişmek için dahili uç nokta kullanın. OSS bölgeleri ve uç noktaları hakkında daha fazla bilgi için bkz. Bölgeler ve uç noktalar.
Bu konuda, bir OSS uç noktası kullanılarak bir OSSClient örneğinin nasıl oluşturulacağı gösterilmektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. OssClient örneği oluşturma.
Yaşam döngüsü kurallarını yapılandırmak için
oss:PutBucketLifecycleiznine sahip olmanız gerekir. Yaşam döngüsü kurallarını sorgulamak içinoss:GetBucketLifecycleiznine sahip olmanız gerekir. Yaşam döngüsü kurallarını temizlemek içinoss:DeleteBucketLifecycleiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
Yaşam döngüsü kuralları ayarlama
Aşağıdaki kod, examplebucket paketi için son değiştirilme zamanına dayalı yaşam döngüsü kurallarının nasıl ayarlanacağını göstermektedir. Kuralları ayarladıktan sonra birini veya birkaçını değiştirmek isterseniz bkz. Bir veya daha fazla yaşam döngüsü kuralını nasıl değiştiririm?
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
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);
SetBucketLifecycleRequest request(BucketName);
std::string date("2022-10-12T00:00:00.000Z");
/* Set tags. */
Tagging tagging;
tagging.addTag(Tag("key1", "value1"));
tagging.addTag(Tag("key2", "value2"));
/* Specify a lifecycle rule. */
auto rule1 = LifecycleRule();
rule1.setID("rule1");
rule1.setPrefix("test1/");
rule1.setStatus(RuleStatus::Enabled);
rule1.setExpiration(3);
rule1.setTags(tagging.Tags());
/* Specify the expiration time. */
auto rule2 = LifecycleRule();
rule2.setID("rule2");
rule2.setPrefix("test2/");
rule2.setStatus(RuleStatus::Disabled);
rule2.setExpiration(date);
/* rule3 is a lifecycle rule for a bucket with versioning enabled. */
auto rule3 = LifecycleRule();
rule3.setID("rule3");
rule3.setPrefix("test3/");
rule3.setStatus(RuleStatus::Disabled);
/* Transition objects to the Archive storage class 365 days after they are last modified. */
auto transition = LifeCycleTransition();
transition.Expiration().setDays(365);
transition.setStorageClass(StorageClass::Archive);
rule3.addTransition(transition);
/* Automatically remove expired delete markers. */
rule3.setExpiredObjectDeleteMarker(true);
/* Transition noncurrent versions of objects to the Infrequent Access storage class 10 days after they become noncurrent. */
auto transition1 = LifeCycleTransition();
transition1.Expiration().setDays(10);
transition1.setStorageClass(StorageClass::IA);
/* Transition noncurrent versions of objects to the Archive storage class 20 days after they become noncurrent. */
auto transition2 = LifeCycleTransition();
transition2.Expiration().setDays(20);
transition2.setStorageClass(StorageClass::Archive);
/* Delete objects 30 days after they become noncurrent versions. */
auto expiration = LifeCycleExpiration(30);
rule3.setNoncurrentVersionExpiration(expiration);
LifeCycleTransitionList noncurrentVersionStorageTransitions{transition1, transition2};
rule3.setNoncurrentVersionTransitionList(noncurrentVersionStorageTransitions);
/* Set the lifecycle rules. */
LifecycleRuleList list{rule1, rule2, rule3};
request.setLifecycleRules(list);
auto outcome = client.SetBucketLifecycle(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "SetBucketLifecycle fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Yaşam döngüsü kurallarını görüntüleme
Aşağıdaki kod, examplebucket adlı paket için yapılandırılmış yaşam döngüsü kurallarının nasıl sorgulanacağını göstermektedir:
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
std::string ToStorageClassName(const StorageClass& storageClass) {
switch (storageClass) {
case StorageClass::Standard:
return "Standard";
case StorageClass::IA:
return "IA";
case StorageClass::Archive:
return "Archive";
case StorageClass::ColdArchive:
return "ColdArchive";
default:
return "Unknown";
}
}
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
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);
/* View the lifecycle rules. */
auto outcome = client.GetBucketLifecycle(BucketName);
if (outcome.isSuccess()) {
std::cout << "GetBucketLifecycle success," << std::endl;
for (auto const rule : outcome.result().LifecycleRules()) {
std::cout << "rule:" << rule.ID() << "," << rule.Prefix() << "," << rule.Status() << ","
"hasExpiration:" << rule.hasExpiration() << "," <<
"hasTransitionList:" << rule.hasTransitionList() << "," << std::endl;
auto taglist = rule.Tags();
for (const auto& tag : taglist)
{
std::cout << "GetBucketLifecycle tag success, Key:"
<< tag.Key() << "; Value:" << tag.Value() << std::endl;
}
/* Check whether to automatically remove expired delete markers. */
if (rule.ExpiredObjectDeleteMarker()) {
std::cout << "rule expired delete marker: " << rule.ExpiredObjectDeleteMarker() << std::endl;
}
/* View the storage class transition rules for noncurrent versions of objects. */
if (rule.hasNoncurrentVersionTransitionList()) {
for (auto const lifeCycleTransition : rule.NoncurrentVersionTransitionList()) {
std::cout << "rule noncurrent versions trans days:" << std::to_string(lifeCycleTransition.Expiration().Days()) <<
" trans storage class: " << ToStorageClassName(lifeCycleTransition.StorageClass()) << std::endl;
}
}
/* View the expiration rules for noncurrent versions of objects. */
if (rule.hasNoncurrentVersionExpiration()) {
std::cout << "rule noncurrent versions expiration days:" << rule.NoncurrentVersionExpiration().Days() << std::endl;
}
}
}
else {
/* Handle exceptions. */
std::cout << "GetBucketLifecycle fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
Yaşam döngüsü kurallarını temizleme
Aşağıdaki kod, examplebucket adlı bir paketteki yaşam döngüsü kurallarının nasıl temizleneceğini göstermektedir. Bir veya daha fazla yaşam döngüsü kuralını silmek isterseniz bakın Yaşam döngüsü kurallarını silme.
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
{
/* Initialize the OSS account information. */
/* Set yourEndpoint to the Endpoint of the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. */
std::string Endpoint = "yourEndpoint";
/* Set yourRegion to the region where the bucket is located. For example, if the bucket is in the China (Hangzhou) region, set the Region to cn-hangzhou. */
std::string Region = "yourRegion";
/* Specify the bucket name. For example, examplebucket. */
std::string BucketName = "examplebucket";
/* Initialize network resources. */
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);
/* Clear the lifecycle rules. */
DeleteBucketLifecycleRequest request(BucketName);
auto outcome = client.DeleteBucketLifecycle(request);
if (!outcome.isSuccess()) {
/* Handle exceptions. */
std::cout << "DeleteBucketLifecycle fail" <<
",code:" << outcome.error().Code() <<
",message:" << outcome.error().Message() <<
",requestId:" << outcome.error().RequestId() << std::endl;
return -1;
}
/* Release network resources. */
ShutdownSdk();
return 0;
}
İlgili Başlıklar
Yaşam döngüsü kuralları için tam örnek kodu görmek üzere GitHub örneği sayfasına gidin.
Yaşam döngüsü kurallarını ayarlamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketLifecycle.
Yaşam döngüsü kurallarını görüntülemek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucketLifecycle.
Tüm yaşam döngüsü kurallarını silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketLifecycle.