6 "github.com/aliyun/aliyun-oss-go-sdk/oss"
9 // BucketLoggingSample shows how to set, get and delete the bucket logging configuration
10 func BucketLoggingSample() {
12 client, err := oss.New(endpoint, accessID, accessKey)
17 // Create the bucket with default parameters
18 err = client.CreateBucket(bucketName)
22 // Create target bucket to store the logging files.
23 var targetBucketName = "target-bucket"
24 err = client.CreateBucket(targetBucketName)
29 // Case 1: Set the logging for the object prefixed with "prefix-1" and save their access logs to the target bucket
30 err = client.SetBucketLogging(bucketName, targetBucketName, "prefix-1", true)
35 // Case 2: Set the logging for the object prefixed with "prefix-2" and save their logs to the same bucket
36 // Note: the rule will overwrite other rules if they have same bucket and prefix
37 err = client.SetBucketLogging(bucketName, bucketName, "prefix-2", true)
42 // Delete the bucket's logging configuration
43 err = client.DeleteBucketLogging(bucketName)
48 // Case 3: Set the logging without enabling it
49 err = client.SetBucketLogging(bucketName, targetBucketName, "prefix-3", false)
54 // Get the bucket's logging configuration
55 gbl, err := client.GetBucketLogging(bucketName)
59 fmt.Println("Bucket Logging:", gbl.LoggingEnabled)
61 err = client.SetBucketLogging(bucketName, bucketName, "prefix2", true)
66 // Get the bucket's logging configuration
67 gbl, err = client.GetBucketLogging(bucketName)
71 fmt.Println("Bucket Logging:", gbl.LoggingEnabled)
73 // Delete the bucket's logging configuration
74 err = client.DeleteBucketLogging(bucketName)
80 err = client.DeleteBucket(bucketName)
84 err = client.DeleteBucket(targetBucketName)
89 fmt.Println("BucketLoggingSample completed")