TOS 支持日志分析功能,支持通过日志服务检索分析您访问 TOS 过程中产生的访问日志。通过 TOS Go SDK 您可以设置日志分析功能配置。
注意
以下代码用于设置桶的实时日志配置规则。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) input := &tos.PutBucketRealTimeLogInput{ Bucket: bucketName, Configuration: tos.RealTimeLogConfiguration{ // TLS Role 的配置 Role: "TOSLogTLSRole", // TLS 的配置 Configuration: tos.AccessLogConfiguration{ UseServiceTopic: true, TLSProjectID: "TLSProjectID", TLSTopicID: "TLSTopicID", }, }, } output, err := client.PutBucketRealTimeLog(context.Background(), input) checkErr(err) fmt.Println("PutBucketRealTimeLog Request ID:", output.RequestID) }
注意
要获取桶的实时日志配置规则,默认您必须为桶所有者。
以下代码用于获取桶的实时日志配置规则。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) output, err := client.GetBucketRealTimeLog(context.Background(), &tos.GetBucketRealTimeLogInput{Bucket: bucketName}) checkErr(err) fmt.Println("GetBucketRealTimeLog Request ID:", output.RequestID) // 查看 TLS 日志配置 fmt.Println("GetBucketRealTimeLog Role", output.Configuration.Role) fmt.Println("GetBucketRealTimeLog TLSTopicID", output.Configuration.Configuration.TLSTopicID) fmt.Println("GetBucketRealTimeLog TLSProjectID", output.Configuration.Configuration.TLSProjectID) fmt.Println("GetBucketRealTimeLog UseServiceTopic", output.Configuration.Configuration.UseServiceTopic) }
注意
要删除桶的实时日志配置规则,默认您必须为桶所有者。
以下代码用于删除桶的实时日志配置规则。
package main import ( "context" "fmt" "github.com/volcengine/ve-tos-golang-sdk/v2/tos" ) func checkErr(err error) { if err != nil { if serverErr, ok := err.(*tos.TosServerError); ok { fmt.Println("Error:", serverErr.Error()) fmt.Println("Request ID:", serverErr.RequestID) fmt.Println("Response Status Code:", serverErr.StatusCode) fmt.Println("Response Header:", serverErr.Header) fmt.Println("Response Err Code:", serverErr.Code) fmt.Println("Response Err Msg:", serverErr.Message) } else { fmt.Println("Error:", err) } panic(err) } } func main() { var ( accessKey = os.Getenv("TOS_ACCESS_KEY") secretKey = os.Getenv("TOS_SECRET_KEY") // Bucket 对于的 Endpoint,以华北2(北京)为例:https://tos-cn-beijing.volces.com endpoint = "https://tos-cn-beijing.volces.com" region = "cn-beijing" // 填写 BucketName bucketName = "*** Provide your bucket name ***" ) // 初始化客户端 client, err := tos.NewClientV2(endpoint, tos.WithRegion(region), tos.WithCredentials(tos.NewStaticCredentials(accessKey, secretKey))) checkErr(err) output, err := client.DeleteBucketRealTimeLog(context.Background(), &tos.DeleteBucketRealTimeLogInput{Bucket: bucketName}) checkErr(err) fmt.Println("DeleteBucketRealTimeLog Request ID:", output.RequestID) }
关于实时日志配置的更多信息,请参见日志分析。