英文:
Need help understanding golang code in opensource project
问题
我使用一个名为AWS-nuke的开源工具来从AWS中删除资源。我想了解一下这个工具在删除S3存储桶之前是否修改了“启用日志记录”功能。
我找到了这段代码,实际上似乎是在修改要删除的存储桶的属性,但我对golang语法不太熟悉,无法理解它实际上在做什么,我希望能够解释一下这段小代码(对于不会编写golang代码的人)!
这是完整的代码:https://github.com/rebuy-de/aws-nuke/blob/f7337e5c069b424605f80eac24f117a6394fb410/resources/s3-buckets.go#L93
我感兴趣的部分是:
_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
Bucket: &e.name,
BucketLoggingStatus: &s3.BucketLoggingStatus{},
})
if err != nil {
return err
}
还有这个结构体定义:
type S3Bucket struct {
svc *s3.S3
name string
creationDate time.Time
tags []*s3.Tag
}
我无法理解&s3.BucketLoggingStatus{}
是什么意思,为什么(我认为)这会在删除存储桶之前禁用存储桶的日志记录状态。
谢谢阅读和帮助!
英文:
I use an open-source tool called AWS-nuke to delete resources from AWS. I wanted to understand if the tool is modifying the "logging enabled" feature of S3 buckets before deleting them.
I found this piece of code that, in fact, seems to be doing so and modifying the property in the bucket to be deleted but I am not very familiarized with golang syntax and I cannot understand what is it actually doing, I would appreciate an explanation (for someone who doesn't code in golang) of this small piece of code!
This is the full code: https://github.com/rebuy-de/aws-nuke/blob/f7337e5c069b424605f80eac24f117a6394fb410/resources/s3-buckets.go#L93
The part I am interested in is:
_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
Bucket: &e.name,
BucketLoggingStatus: &s3.BucketLoggingStatus{},
})
if err != nil {
return err
}
and there is also this struct definition:
type S3Bucket struct {
svc *s3.S3
name string
creationDate time.Time
tags []*s3.Tag
}
I can't understand what is &s3.BucketLoggingStatus{}
and why (I think) this is disabling the logging status of the bucket before deleting it.
Thanks for reading and the help!
答案1
得分: 2
根据这段完整的代码:https://github.com/rebuy-de/aws-nuke/blob/f7337e5c069b424605f80eac24f117a6394fb410/resources/s3-buckets.go#L93
它的作用是:
- 移除所有的存储桶策略
- 禁用日志记录
- 移除版本
- 移除对象
在删除实际存储桶之前。
现在,这段代码片段的含义是:
_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
Bucket: &e.name,
BucketLoggingStatus: &s3.BucketLoggingStatus{},
})
if err != nil {
return err
}
禁用存储桶的日志记录(因为&s3.BucketLoggingStatus{}是一个空结构体)。假设你不想删除存储桶,而是想启用日志记录。你可以这样做:
type LoggingEnabled struct {
TargetBucket *string `type:"string" required:"true"`
TargetGrants []*TargetGrant `locationNameList:"Grant" type:"list"`
TargetPrefix *string `type:"string" required:"true"`
}
_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
Bucket: &e.name,
BucketLoggingStatus: &s3.BucketLoggingStatus{
LoggingEnabled: &LoggingEnabled
},
})
if err != nil {
return err
}
英文:
As per this full code:
https://github.com/rebuy-de/aws-nuke/blob/f7337e5c069b424605f80eac24f117a6394fb410/resources/s3-buckets.go#L93
It is trying to :
- remove all bucket policy
- disable logging
- remove versions and
- remove objects
before deleting the actual bucket.
Now, the meaning of this code snippet is,
_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
Bucket: &e.name,
BucketLoggingStatus: &s3.BucketLoggingStatus{},
})
if err != nil {
return err
}
disabling the Logging of buckets (becasue &s3.BucketLoggingStatus{} this is empty struct). Let's suppose you don't want to delete bucket and wanted to enable the logging. So you can do like,
type LoggingEnabled struct {
TargetBucket *string `type:"string" required:"true"`
TargetGrants []*TargetGrant `locationNameList:"Grant" type:"list"`
TargetPrefix *string `type:"string" required:"true"`
}
_, err = e.svc.PutBucketLogging(&s3.PutBucketLoggingInput{
Bucket: &e.name,
BucketLoggingStatus: &s3.BucketLoggingStatus{
LoggingEnabled: &LoggingEnabled
},
})
if err != nil {
return err
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论