OSDN Git Service

Create ossClient.go (#574)
[bytom/vapor.git] / vendor / github.com / aliyun / aliyun-oss-go-sdk / sample / bucket_logging.go
1 package sample
2
3 import (
4         "fmt"
5
6         "github.com/aliyun/aliyun-oss-go-sdk/oss"
7 )
8
9 // BucketLoggingSample shows how to set, get and delete the bucket logging configuration
10 func BucketLoggingSample() {
11         // New client
12         client, err := oss.New(endpoint, accessID, accessKey)
13         if err != nil {
14                 HandleError(err)
15         }
16
17         // Create the bucket with default parameters
18         err = client.CreateBucket(bucketName)
19         if err != nil {
20                 HandleError(err)
21         }
22         // Create target bucket to store the logging files.
23         var targetBucketName = "target-bucket"
24         err = client.CreateBucket(targetBucketName)
25         if err != nil {
26                 HandleError(err)
27         }
28
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)
31         if err != nil {
32                 HandleError(err)
33         }
34
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)
38         if err != nil {
39                 HandleError(err)
40         }
41
42         // Delete the bucket's logging configuration
43         err = client.DeleteBucketLogging(bucketName)
44         if err != nil {
45                 HandleError(err)
46         }
47
48         // Case 3: Set the logging without enabling it
49         err = client.SetBucketLogging(bucketName, targetBucketName, "prefix-3", false)
50         if err != nil {
51                 HandleError(err)
52         }
53
54         // Get the bucket's logging configuration
55         gbl, err := client.GetBucketLogging(bucketName)
56         if err != nil {
57                 HandleError(err)
58         }
59         fmt.Println("Bucket Logging:", gbl.LoggingEnabled)
60
61         err = client.SetBucketLogging(bucketName, bucketName, "prefix2", true)
62         if err != nil {
63                 HandleError(err)
64         }
65
66         // Get the bucket's logging configuration
67         gbl, err = client.GetBucketLogging(bucketName)
68         if err != nil {
69                 HandleError(err)
70         }
71         fmt.Println("Bucket Logging:", gbl.LoggingEnabled)
72
73         // Delete the bucket's logging configuration
74         err = client.DeleteBucketLogging(bucketName)
75         if err != nil {
76                 HandleError(err)
77         }
78
79         // Delete bucket
80         err = client.DeleteBucket(bucketName)
81         if err != nil {
82                 HandleError(err)
83         }
84         err = client.DeleteBucket(targetBucketName)
85         if err != nil {
86                 HandleError(err)
87         }
88
89         fmt.Println("BucketLoggingSample completed")
90 }