Object Storage Service (OSS) paketindeki nesnelerin erişim desenlerini otomatik olarak izlemek ve bu nesneleri daha maliyet etkin depolama sınıflarına taşımak amacıyla son erişim zamanı ilkesine dayalı yaşam döngüsü kuralları yapılandırmak için öncelikle erişim izlemeyi etkinleştirmeniz gerekir.
Bir nesnenin en son ne zaman erişildiğini sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetObjectMeta.
Notlar
Bu konuda Çin (Hangzhou) bölgesinin genel uç noktası kullanılmıştır. OSS ile aynı bölgedeki diğer Alibaba Cloud hizmetlerinden OSS'e 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ınmaktadı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ı kullanarak bir OSSClient örneği oluşturmayı göstermektedir. Özel etki alanı kullanma veya Security Token Service (STS) kimlik bilgileriyle kimlik doğrulama gibi alternatif yapılandırmalar için bkz. İstemciyi yapılandırma (Go SDK V1).
Erişim izlemeyi etkinleştirin
Aşağıdaki örnek kod, examplebucket adlı bir paket için erişim izlemenin nasıl etkinleştirileceğini göstermektedir:
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 configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
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 name of the bucket.
bucketName := "examplebucket"
// Enable access tracking for examplebucket.
access := oss.PutBucketAccessMonitor{
Status: "Enabled",
}
err = client.PutBucketAccessMonitor(bucketName, access)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Println("put bucket access monitor success!")
}
Erişim izleme durumunu sorgulayın
Aşağıdaki örnek kod, examplebucket adlı bir paketin erişim izleme durumunun nasıl sorgulanacağını göstermektedir:
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 configured.
provider, err := oss.NewEnvironmentVariableCredentialsProvider()
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
// Create an OSSClient instance.
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. Specify your actual endpoint.
// Specify the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the region to cn-hangzhou. Specify the actual region.
clientOptions := []oss.ClientOption{oss.SetCredentialsProvider(&provider)}
clientOptions = append(clientOptions, oss.Region("yourRegion"))
// Specify the version of the signature algorithm.
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 name of the bucket.
bucketName := "examplebucket"
// View the access tracking status of examplebucket.
result, err := client.GetBucketAccessMonitor(bucketName)
if err != nil {
fmt.Println("Error:", err)
os.Exit(-1)
}
fmt.Printf("bucket access monitor config is:%s\n", result.Status)
}
İlgili Konular
Son erişim zamanına dayalı yaşam döngüsü kuralları hakkında daha fazla bilgi için bkz. Son erişim zamanına dayalı yaşam döngüsü kuralları.
Erişim izlemeyi etkinleştirmek için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. PutBucketAccessMonitor.
Erişim izleme durumunu sorgulamak için çağırabileceğiniz API işlemi hakkında daha fazla bilgi için bkz. GetBucketAccessMonitor.