Object Storage Service (OSS) üzerine yüklenen tüm veriler sık erişim gerektirmez. Veri uyumluluğu veya arşivleme gibi nedenlerle bazı verileri soğuk depolama sınıfında saklamanız gerekebilir. Artık ihtiyaç duymadığınız verileri toplu iş olarak silmek için son değiştirilme zamanına dayalı bir yaşam döngüsü ilkesi yapılandırabilirsiniz. OSS'nin veri erişim modellerini otomatik olarak izlemesini, soğuk verileri algılamasını ve bu verileri farklı bir depolama sınıfına geçirmesini sağlamak için son erişim zamanına dayalı bir yaşam döngüsü ilkesi yapılandırabilirsiniz. Bu süreç, Otomatik Depolama Katmanlama uygulamasını gerçekleştirir ve depolama maliyetlerinizi azaltır.
Notlar
Nesnelerin son değiştirilme zamanına veya son erişim zamanına dayalı yaşam döngüsü ilkelerini yapılandırmadan önce bu özelliği iyi bildiğinizden emin olun. Daha fazla bilgi için bkz. Son değiştirilme zamanına dayalı yaşam döngüsü ilkeleri ve Son erişim zamanına dayalı yaşam döngüsü ilkeleri.
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'ye erişmek için dahili bir 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 erişim kimlik bilgileri ortam değişkenlerinden alınır. Erişim kimlik bilgilerinin nasıl yapılandırılacağı hakkında daha fazla bilgi için bkz. Erişim kimlik bilgilerini yapılandırma.
Bu konu, bir OSS uç noktası ile bir OSSClient örneğinin oluşturulmasını göstermektedir. Özel bir alan adı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemci yapılandırma (Go SDK V1).
Yaşam döngüsü ilkelerini yapılandırmak için
oss:PutBucketLifecycleiznine sahip olmanız gerekir. Yaşam döngüsü ilkelerini sorgulamak içinoss:GetBucketLifecycleiznine sahip olmanız gerekir. Yaşam döngüsü ilkelerini temizlemek içinoss:DeleteBucketLifecycleiznine sahip olmanız gerekir. Daha fazla bilgi için bkz. Özel ilke atama.
Yaşam döngüsü ilkelerini ayarlayın
Aşağıdaki kod, son değiştirilme zamanına ve son erişim zamanına dayalı yaşam döngüsü ilkelerinin nasıl ayarlanacağına ilişkin örnekler sağlar. Ayarladıktan sonra bir veya daha fazla kuralı değiştirmek için bkz. Bir veya daha fazla yaşam döngüsü ilkesi yapılandırmasını nasıl değiştiririm?.
Son değiştirilme zamanına göre depolama sınıflarını geçirin ve dosyaları silin
Aşağıdaki kod, examplebucket adlı bir paket için depolama sınıflarını geçirmek ve dosyaları son değiştirilme zamanına göre silmek üzere bir yaşam döngüsü ilkesinin nasıl ayarlanacağına ilişkin bir örnek sağlar.
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Specify the bucket name.
bucketName := "examplebucket"
// Specify lifecycle rule 1. This rule specifies that files with the prefix "foo/" expire 3 days after they are last modified.
rule1 := oss.BuildLifecycleRuleByDays("rule1", "foo/", true, 3)
// Automatically delete delete markers for objects in a versioning-enabled bucket when the objects have only delete markers.
deleteMark := true
expiration := oss.LifecycleExpiration{
ExpiredObjectDeleteMarker: &deleteMark,
}
// Delete noncurrent versions of objects 30 days after they become noncurrent.
versionExpiration := oss.LifecycleVersionExpiration{
NoncurrentDays: 30,
}
// Transition noncurrent versions of objects to the Infrequent Access (IA) storage class 10 days after they become noncurrent.
versionTransition := oss.LifecycleVersionTransition{
NoncurrentDays: 10,
StorageClass: "IA",
}
// Specify lifecycle rule 2.
rule2 := oss.LifecycleRule{
ID: "rule2",
Prefix: "yourObjectPrefix",
Status: "Enabled",
Expiration: &expiration,
NonVersionExpiration: &versionExpiration,
NonVersionTransitions: []oss.LifecycleVersionTransition{
versionTransition,
},
}
// Specify lifecycle rule 3. This rule specifies that files with the tag key "tag1" and tag value "value1" expire 3 days after they are last modified.
rule3 := oss.LifecycleRule{
ID: "rule3",
Prefix: "",
Status: "Enabled",
Tags: []oss.Tag{
{
Key: "tag1",
Value: "value1",
},
},
Expiration: &oss.LifecycleExpiration{Days: 3},
}
// Set the lifecycle rules.
rules := []oss.LifecycleRule{rule1, rule2, rule3}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Belirli bir ön ek ve etikete sahip olmayan dosyaların depolama sınıfını son değiştirilme zamanına göre geçirin
Aşağıdaki kod, filter düğümü altındaki Not öğesini kullanarak examplebucket içindeki tüm dosyaları, log ön ekine, key1 anahtarına ve value1 değerine sahip bir etikete ve belirtilen bir boyuta sahip dosyalar hariç olmak üzere, son değiştirilme zamanından 30 gün sonra Sık Erişilmeyen depolama sınıfına geçirir.
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Set yourBucketName to the name of your bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
tag := oss.Tag{
Key: "key1",
Value: "value1",
}
// Specify the minimum size of objects to which the lifecycle rule applies.
size := int64(500)
// Specify the maximum size of objects to which the lifecycle rule applies.
smallSize := int64(64500)
filter := oss.LifecycleFilter{
ObjectSizeGreaterThan: &size,
ObjectSizeLessThan: &smallSize,
Not: []oss.LifecycleFilterNot{
{
Prefix: "logs/log",
Tag: &tag,
},
},
}
rule1 := oss.LifecycleRule{
ID: "mtime transition1",
Prefix: "logs",
Status: "Enabled",
Transitions: []oss.LifecycleTransition{
{
Days: 30,
StorageClass: oss.StorageIA,
},
},
Filter: &filter,
}
var rules = []oss.LifecycleRule{rule1}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("%s\n", "set lifecycle succeeded")
}
Son erişim zamanına göre depolama sınıflarını geçirin
Aşağıdaki kod, yourBucketName adlı bir paket için depolama sınıflarını son erişim zamanına göre geçirmek üzere bir yaşam döngüsü ilkesinin nasıl ayarlanacağına ilişkin bir örnek sağlar.
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Set yourBucketName to the name of your bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
isTrue := true
isFalse := false
// Specify lifecycle rule 1. This rule transitions all objects that have the prefix "logs" and are 64 KB or smaller to the Infrequent Access storage class 30 days after they are last accessed. When these objects are accessed again, they remain in the Infrequent Access storage class.
rule1 := oss.LifecycleRule{
ID: "rule1",
Prefix: "logs",
Status: "Enabled",
Transitions: []oss.LifecycleTransition{
{
Days: 30,
StorageClass: oss.StorageIA,
IsAccessTime: &isTrue,
ReturnToStdWhenVisit: &isFalse,
AllowSmallFile: &isTrue,
},
},
}
// Specify lifecycle rule 2. This rule transitions all noncurrent versions of objects that have the prefix "dir" and are larger than 64 KB to the Infrequent Access storage class 10 days after they are last accessed. When these objects are accessed again, they are transitioned back to the Standard storage class.
rule2 := oss.LifecycleRule{
ID: "rule2",
Prefix: "dir",
Status: "Enabled",
NonVersionTransitions: []oss.LifecycleVersionTransition{
{
NoncurrentDays: 10,
StorageClass: oss.StorageIA,
IsAccessTime: &isTrue,
ReturnToStdWhenVisit: &isTrue,
AllowSmallFile: &isFalse,
},
},
}
// Set the lifecycle rules.
var rules = []oss.LifecycleRule{rule1, rule2}
err = client.SetBucketLifecycle(bucketName, rules)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("%s\n", "set bucket lifecycle success")
}
Yaşam döngüsü ilkelerini görüntüleyin
Aşağıdaki kod, examplebucket adlı paket için yapılandırılan yaşam döngüsü ilkelerinin nasıl sorgulanacağına ilişkin bir örnek sağlar:
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Set yourBucketName to the name of your bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
lcRes, err := client.GetBucketLifecycle(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
for _, rule := range lcRes.Rules {
fmt.Println("Lifecycle Rule Id:", rule.ID)
fmt.Println("Lifecycle Rule Prefix:", rule.Prefix)
fmt.Println("Lifecycle Rule Status:", rule.Status)
if rule.Expiration != nil {
fmt.Println("Lifecycle Rule Expiration Days:", rule.Expiration.Days)
fmt.Println("Lifecycle Rule Expiration Date:", rule.Expiration.Date)
fmt.Println("Lifecycle Rule Expiration Created Before Date:", rule.Expiration.CreatedBeforeDate)
if rule.Expiration.ExpiredObjectDeleteMarker != nil {
fmt.Println("Lifecycle Rule Expiration Expired Object DeleteMarker:", *rule.Expiration.ExpiredObjectDeleteMarker)
}
}
for _, tag := range rule.Tags {
fmt.Println("Lifecycle Rule Tag Key:", tag.Key)
fmt.Println("Lifecycle Rule Tag Value:", tag.Value)
}
for _, transition := range rule.Transitions {
fmt.Println("Lifecycle Rule Transition Days:", transition.Days)
fmt.Println("Lifecycle Rule Transition Created Before Date:", transition.CreatedBeforeDate)
fmt.Println("Lifecycle Rule Transition Storage Class:", transition.StorageClass)
if transition.IsAccessTime != nil {
fmt.Println("Lifecycle Rule Transition Is Access Time:", *transition.IsAccessTime)
}
if transition.ReturnToStdWhenVisit != nil {
fmt.Println("Lifecycle Rule Transition Return To Std When Visit:", *transition.ReturnToStdWhenVisit)
}
if transition.AllowSmallFile != nil {
fmt.Println("Lifecycle Rule Transition Allow Small File:", *transition.AllowSmallFile)
}
}
if rule.AbortMultipartUpload != nil {
fmt.Println("Lifecycle Rule Abort Multipart Upload Days:", rule.AbortMultipartUpload.Days)
fmt.Println("Lifecycle Rule Abort Multipart Upload Created Before Date:", rule.AbortMultipartUpload.CreatedBeforeDate)
}
if rule.NonVersionExpiration != nil {
fmt.Println("Lifecycle Non Version Expiration Non Current Days:", rule.NonVersionExpiration.NoncurrentDays)
}
for _, nonVersionTransition := range rule.NonVersionTransitions {
fmt.Println("Lifecycle Rule Non Version Transitions Non current Days:", nonVersionTransition.NoncurrentDays)
fmt.Println("Lifecycle Rule Non Version Transition Storage Class:", nonVersionTransition.StorageClass)
if nonVersionTransition.IsAccessTime != nil {
fmt.Println("Lifecycle Rule Non Version Transition Is Access Time:", *nonVersionTransition.IsAccessTime)
}
if nonVersionTransition.ReturnToStdWhenVisit != nil {
fmt.Println("Lifecycle Rule Non Version Transition Return To Std When Visit:", *nonVersionTransition.ReturnToStdWhenVisit)
}
if nonVersionTransition.AllowSmallFile != nil {
fmt.Println("Lifecycle Rule Non Version Allow Small File:", *nonVersionTransition.AllowSmallFile)
}
if rule.Filter != nil {
if rule.Filter.ObjectSizeGreaterThan != nil {
fmt.Println("Lifecycle Rule Filter Object Size Greater Than:", *rule.Filter.ObjectSizeGreaterThan)
}
if rule.Filter.ObjectSizeLessThan != nil {
fmt.Println("Lifecycle Rule Filter Object Size Less Than:", *rule.Filter.ObjectSizeLessThan)
}
for _, filterNot := range rule.Filter.Not {
fmt.Println("Lifecycle Rule Filter Not Prefix:", filterNot.Prefix)
if filterNot.Tag != nil {
fmt.Println("Lifecycle Rule Filter Not Tag Key:", filterNot.Tag.Key)
fmt.Println("Lifecycle Rule Filter Not Tag Value:", filterNot.Tag.Value)
}
}
}
}
}
}
Yaşam döngüsü ilkelerini temizleyin
Aşağıdaki kod, examplebucket adlı bir paketteki yaşam döngüsü ilkelerinin nasıl temizleneceğine ilişkin bir örnek sağlar. Bir veya daha fazla yaşam döngüsü ilkesini silmek istiyorsanız bakın Yaşam döngüsü ilkelerini silme.
package main
import (
"fmt"
"os"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
func main() {
// Set yourBucketName to the name of your bucket.
bucketName := "yourBucketName"
// Obtain access credentials from environment variables. Before you run the sample code, make sure that the OSS_ACCESS_KEY_ID and OSS_ACCESS_KEY_SECRET environment variables are set.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Set yourEndpoint to the Endpoint of the bucket. For example, for the China (Hangzhou) region, set the Endpoint to https://oss-cn-hangzhou.aliyuncs.com. For other regions, use the actual Endpoint.
// Set yourRegion to the region where the bucket is located. For example, for the China (Hangzhou) region, set the region to cn-hangzhou. For other regions, use the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Set the signature version.
clientOptions = append(clientOptions, oss.AuthVersion(oss.AuthV4))
client, err := oss.New("yourEndpoint", "", "", clientOptions...)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Delete all lifecycle rules.
err = client.DeleteBucketLifecycle(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
}
Başvurular
Yaşam döngüsü ilkeleri için eksiksiz örnek kod için GitHub'daki örneğe bakın.
Yaşam döngüsü ilkelerini ayarlamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketLifecycle.
Yaşam döngüsü ilkelerini 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ü ilkelerini silmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. DeleteBucketLifecycle.