edit-icon download-icon

Set logging

Last Updated: Nov 07, 2017

OSS allows you to configure access logging for a bucket. After the configuration, accesses to the bucket are logged. The logs are stored in a specified bucket on OSS in the format of:

  1. <TargetPrefix><SourceBucket>-YYYY-mm-DD-HH-MM-SS-UniqueString

You must specify the TargetPrefix. A logging rule consists of the following three parts:

  • enable: Whether to enable access logging
  • target_bucket: The bucket where logs are stored
  • target_prefix: The prefix of the stored log objects

For more information about access logging, see Set access logging.

Note: For the example code of bucket ACL settings, see sample/bucket_logging.go.

Enable bucket logging

You can use Client.SetBucketLogging to enable bucket logging:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. // target_bucket:"my-target-bucket", target_prefix:"my-object-", enable: true
  7. err = client.SetBucketLogging("my-bucket", "my-target-bucket", "my-object-", true)
  8. if err != nil {
  9. // HandleError(err)
  10. }

View bucket logging settings

You can use Client.GetBucketLogging to view bucket logging settings:

  1. import "fmt"
  2. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  3. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  4. if err != nil {
  5. // HandleError(err)
  6. }
  7. logRes, err := client.GetBucketLogging("my-bucket")
  8. if err != nil {
  9. // HandleError(err)
  10. }
  11. fmt.Println("Target Bucket:", logRes.LoggingEnabled.TargetBucket,
  12. "Target Prefix:", logRes.LoggingEnabled.TargetPrefix)

Disable bucket logging

You can use Bucket.DeleteBucketLogging to disable bucket logging:

  1. import "github.com/aliyun/aliyun-oss-go-sdk/oss"
  2. client, err := oss.New("Endpoint", "AccessKeyId", "AccessKeySecret")
  3. if err != nil {
  4. // HandleError(err)
  5. }
  6. err = client.DeleteBucketLogging("my-bucket")
  7. if err != nil {
  8. // HandleError(err)
  9. }
Thank you! We've received your feedback.