123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990 |
- package sample
- import (
- "fmt"
- "github.com/aliyun/aliyun-oss-go-sdk/oss"
- )
- // BucketLoggingSample 展示了如何设置/读取/清除存储空间的日志(Bucket Logging)
- func BucketLoggingSample() {
- // New Client
- client, err := oss.New(endpoint, accessID, accessKey)
- if err != nil {
- HandleError(err)
- }
- // 创建bucket
- err = client.CreateBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- // 创建Target bucket,存储访问日志
- var targetBucketName = "target-bucket"
- err = client.CreateBucket(targetBucketName)
- if err != nil {
- HandleError(err)
- }
- // 场景1:设置Logging,bucketName中以"prefix"为前缀的object的访问日志将被记录到targetBucketName
- err = client.SetBucketLogging(bucketName, targetBucketName, "prefix-1", true)
- if err != nil {
- HandleError(err)
- }
- // 场景2:设置Logging,bucketName中以"prefix"为前缀的object的访问日志将被记录到bucketName
- // 注意:相同bucket,相同prefix,多次设置后者会覆盖前者
- err = client.SetBucketLogging(bucketName, bucketName, "prefix-2", true)
- if err != nil {
- HandleError(err)
- }
- // 删除Bucket上的Logging设置
- err = client.DeleteBucketLogging(bucketName)
- if err != nil {
- HandleError(err)
- }
- // 场景3:设置但不生效
- err = client.SetBucketLogging(bucketName, targetBucketName, "prefix-3", false)
- if err != nil {
- HandleError(err)
- }
- // 获取Bucket上设置的Logging
- gbl, err := client.GetBucketLogging(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("Bucket Logging:", gbl.LoggingEnabled)
- err = client.SetBucketLogging(bucketName, bucketName, "prefix2", true)
- if err != nil {
- HandleError(err)
- }
- // 获取Bucket上设置的Logging
- gbl, err = client.GetBucketLogging(bucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("Bucket Logging:", gbl.LoggingEnabled)
- // 删除Bucket上的Logging设置
- err = client.DeleteBucketLogging(bucketName)
- if err != nil {
- HandleError(err)
- }
- // 删除bucket
- err = client.DeleteBucket(bucketName)
- if err != nil {
- HandleError(err)
- }
- err = client.DeleteBucket(targetBucketName)
- if err != nil {
- HandleError(err)
- }
- fmt.Println("BucketLoggingSample completed")
- }
|