-
Notifications
You must be signed in to change notification settings - Fork 234
/
bucket_logging.go
90 lines (76 loc) · 2.23 KB
/
bucket_logging.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package sample
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
)
// BucketLoggingSample shows how to set, get and delete the bucket logging configuration
func BucketLoggingSample() {
// New client
client, err := oss.New(endpoint, accessID, accessKey)
if err != nil {
HandleError(err)
}
// Create the bucket with default parameters
err = client.CreateBucket(bucketName)
if err != nil {
HandleError(err)
}
// Create target bucket to store the logging files.
var targetBucketName = "target-bucket"
err = client.CreateBucket(targetBucketName)
if err != nil {
HandleError(err)
}
// Case 1: Set the logging for the object prefixed with "prefix-1" and save their access logs to the target bucket
err = client.SetBucketLogging(bucketName, targetBucketName, "prefix-1", true)
if err != nil {
HandleError(err)
}
// Case 2: Set the logging for the object prefixed with "prefix-2" and save their logs to the same bucket
// Note: the rule will overwrite other rules if they have same bucket and prefix
err = client.SetBucketLogging(bucketName, bucketName, "prefix-2", true)
if err != nil {
HandleError(err)
}
// Delete the bucket's logging configuration
err = client.DeleteBucketLogging(bucketName)
if err != nil {
HandleError(err)
}
// Case 3: Set the logging without enabling it
err = client.SetBucketLogging(bucketName, targetBucketName, "prefix-3", false)
if err != nil {
HandleError(err)
}
// Get the bucket's logging configuration
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)
}
// Get the bucket's logging configuration
gbl, err = client.GetBucketLogging(bucketName)
if err != nil {
HandleError(err)
}
fmt.Println("Bucket Logging:", gbl.LoggingEnabled)
// Delete the bucket's logging configuration
err = client.DeleteBucketLogging(bucketName)
if err != nil {
HandleError(err)
}
// Delete bucket
err = client.DeleteBucket(bucketName)
if err != nil {
HandleError(err)
}
err = client.DeleteBucket(targetBucketName)
if err != nil {
HandleError(err)
}
fmt.Println("BucketLoggingSample completed")
}