开启桶日志
更新时间: 2019-03-14 10:05
您可以通过ObsClient.SetBucketLoggingConfiguration开启桶日志功能。
说明:
- 日志目标桶与源桶必须在同一个区域(region)。
- 如果桶的存储类型为低频访问存储或归档存储,则不能作为日志目标桶。
开启桶日志
以下代码展示了如何开启桶日志:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- // 设置桶的日志配置
- input := &obs.SetBucketLoggingConfigurationInput{}
- input.Bucket = "bucketname"
- input.TargetBucket = "targetbucketname"
- input.TargetPrefix = "prefix"
- input.Agency = "your agency"
- output, err := obsClient.SetBucketLoggingConfiguration(input)
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
为日志对象设置权限
以下代码展示了如何为日志对象设置权限:
- // 引入依赖包
- import (
- "fmt"
- "obs"
- )
- var ak = "*** Provide your Access Key ***"
- var sk = "*** Provide your Secret Key ***"
- var endpoint = "https://your-endpoint"
- // 创建ObsClient结构体
- var obsClient, _ = obs.New(ak, sk, endpoint)
- func main() {
- // 设置桶的日志配置
- input := &obs.SetBucketLoggingConfigurationInput{}
- input.Bucket = "bucketname"
- input.TargetBucket = "targetbucketname"
- input.TargetPrefix = "prefix"
- input.Agency = "your agency"
- output, err := obsClient.SetBucketLoggingConfiguration(input)
- var grants [1]obs.Grant
- // 为所有用户设置对日志对象的读权限
- grants[0].Grantee.Type = obs.GranteeGroup
- grants[0].Grantee.URI = obs.GroupAllUsers
- grants[0].Permission = obs.PermissionRead
- input.TargetGrants = grants[:]
- if err == nil {
- fmt.Printf("RequestId:%s\n", output.RequestId)
- } else if obsError, ok := err.(obs.ObsError); ok {
- fmt.Printf("Code:%s\n", obsError.Code)
- fmt.Printf("Message:%s\n", obsError.Message)
- }
- }
父主题:设置访问日志