Object Storage Service (OSS) generates access logs to record access to resources stored in OSS buckets. After you enable logging for a bucket, OSS generates access logs every hour based on predefined naming rules and then stores the logs in the specified bucket.
Usage notes
The sample code in this topic uses the region ID
cn-hangzhouof the China (Hangzhou) region. By default, the public endpoint is used to access resources in a bucket. If you want to access resources in the bucket from other Alibaba Cloud services in the same region in which the bucket is located, we recommend that you use an internal endpoint. For a list of regions and endpoints for OSS, see OSS regions and endpoints.In this topic, access credentials are obtained from environment variables. For more information about how to configure access credentials, see Configure access credentials.
To enable logging for a bucket, you must have the
oss:PutBucketLoggingpermission. To query the logging settings of a bucket, you must have theoss:GetBucketLoggingpermission. To disable logging for a bucket, you must have theoss:DeleteBucketLoggingpermission. For more information about how to grant permissions, see Attach a custom policy to a RAM user.
Sample code
Enable logging
The following code enables logging for a bucket:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
)
// Initialize command-line arguments.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command-line arguments.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configuration and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to enable logging for the bucket.
putRequest := &oss.PutBucketLoggingRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket for which you want to enable logging.
BucketLoggingStatus: &oss.BucketLoggingStatus{
LoggingEnabled: &oss.LoggingEnabled{
TargetBucket: oss.Ptr("TargetBucket"), // Specify the name of the destination bucket in which the log objects are stored. The source and destination buckets can be the same bucket or different buckets in the same region.
TargetPrefix: oss.Ptr("log"), // Specify the directory in which the log objects are stored. If you specify this parameter, the logs are stored in the specified directory of the destination bucket. If you do not specify this parameter, the logs are stored in the root directory of the destination bucket.
},
},
}
// Execute the request.
putResult, err := client.PutBucketLogging(context.TODO(), putRequest)
if err != nil {
log.Fatalf("failed to put bucket logging %v", err)
}
// Display the result.
log.Printf("put bucket logging result:%#v\n", putResult)
}
Query the logging settings of a bucket
The following code queries the logging settings of a bucket:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
)
// Initialize command-line arguments.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command-line arguments.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configuration and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to query the logging settings of the bucket.
getRequest := &oss.GetBucketLoggingRequest{
Bucket: oss.Ptr(bucketName), // The name of the bucket.
}
// Perform the query operation and process the result.
getResult, err := client.GetBucketLogging(context.TODO(), getRequest)
if err != nil {
log.Fatalf("failed to get bucket logging %v", err)
}
// Display the result.
log.Printf("get bucket logging result target bucket:%#v\n", getResult.BucketLoggingStatus.LoggingEnabled.TargetBucket)
}
Disable logging for a bucket
The following code disables logging for a bucket:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Define global variables.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
)
// Initialize command-line arguments.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command-line arguments.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configuration and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Create a request to disable logging for the bucket.
delRequest := &oss.DeleteBucketLoggingRequest{
Bucket: oss.Ptr(bucketName), // Name of the bucket.
}
// Execute the request to disable logging and process the result.
delResult, err := client.DeleteBucketLogging(context.TODO(), delRequest)
if err != nil {
log.Fatalf("failed to delete bucket logging %v", err)
}
// Display the result.
log.Printf("delete bucket logging result:%#v\n", delResult)
}
Configure custom log fields
You can call the PutUserDefinedLogFieldsConfig operation to configure the user_defined_log_fields field, which contains custom log fields. These custom log fields may include request headers or query parameters pertinent to your subsequent request analysis. The following sample code configures custom log fields for a bucket:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Declare variables for storing command-line arguments.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
)
// Initialize command-line arguments.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command-line arguments.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configuration and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Construct a PutUserDefinedLogFieldsConfigRequest.
request := &oss.PutUserDefinedLogFieldsConfigRequest{
Bucket: oss.Ptr(bucketName), // Specify the bucket name.
UserDefinedLogFieldsConfiguration: &oss.UserDefinedLogFieldsConfiguration{
HeaderSet: &oss.LoggingHeaderSet{
Headers: []string{"header1", "header2"}, // Specify HTTP headers to be logged.
},
ParamSet: &oss.LoggingParamSet{
Parameters: []string{"param"}, // Specify query parameters to be logged.
},
},
}
// Execute the request.
result, err := client.PutUserDefinedLogFieldsConfig(context.TODO(), request)
if err != nil {
log.Fatalf("failed to put user defined log fields config %v", err)
}
// Display the result.
log.Printf("put user defined log fields config result:%#v\n", result)
}
Query custom log field settings
You can call the GetUserDefinedLogFieldsConfig operation to query the user_defined_log_fields settings. The following sample code queries the custom log field settings for a bucket:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Declare variables for storing command-line arguments.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
)
// Initialize command-line arguments.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command-line arguments.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configuration and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Construct a GetUserDefinedLogFieldsConfigRequest.
getRequest := &oss.GetUserDefinedLogFieldsConfigRequest{
Bucket: oss.Ptr(bucketName), // 设置目标 Bucket 名称
}
// Execute the request.
getResult, err := client.GetUserDefinedLogFieldsConfig(context.TODO(), getRequest)
if err != nil {
// In case of an error, display it and exit the program.
log.Fatalf("failed to get user defined log fields config %v", err)
}
// Display the result.
log.Printf("get user defined log fields config result:%#v\n", getResult)
}
Delete custom log field settings
You can call the DeleteUserDefinedLogFieldsConfig operation to delete custom user_defined_log_fields settings. The following sample code deletes custom log field settings for a bucket:
package main
import (
"context"
"flag"
"log"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss"
"github.com/aliyun/alibabacloud-oss-go-sdk-v2/oss/credentials"
)
// Declare variables for storing command-line arguments.
var (
region string // The region in which the bucket is located.
bucketName string // The name of the bucket.
)
// Initialize command-line arguments.
func init() {
flag.StringVar(®ion, "region", "", "The region in which the bucket is located.")
flag.StringVar(&bucketName, "bucket", "", "The name of the bucket.")
}
func main() {
// Parse command-line arguments.
flag.Parse()
// Check whether the name of the bucket is specified.
if len(bucketName) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, bucket name required")
}
// Check whether the region is specified.
if len(region) == 0 {
flag.PrintDefaults()
log.Fatalf("invalid parameters, region required")
}
// Load the default configuration and specify the credential provider and region.
cfg := oss.LoadDefaultConfig().
WithCredentialsProvider(credentials.NewEnvironmentVariableCredentialsProvider()).
WithRegion(region)
// Create an OSS client.
client := oss.NewClient(cfg)
// Construct a DeleteUserDefinedLogFieldsConfigRequest.
request := &oss.DeleteUserDefinedLogFieldsConfigRequest{
Bucket: oss.Ptr(bucketName), // Specify the name of the bucket.
}
// Execute the request.
result, err := client.DeleteUserDefinedLogFieldsConfig(context.TODO(), request)
if err != nil {
log.Fatalf("failed to delete user defined log fields config %v", err)
}
// Display the result.
log.Printf("delete user defined log fields config result:%#v\n", result)
}
References
For more information about the API operation that enables logging for a bucket, see PutBucketLogging.
For more information about the API operation that queries the logging settings of a bucket, see GetBucketLogging.
For more information about the API operation that disables logging for a bucket, see DeleteBucketLogging.
For more information about the API operation that configures custom log fields, see PutUserDefinedLogFieldsConfig.
For more information about the API operation that queries custom log field settings, see GetUserDefinedLogFieldsConfig.
For more information about the API operation that deletes custom log field settings, DeleteUserDefinedLogFieldsConfig.