需要帮助理解开源项目中的Golang代码。

huangapple go评论98阅读模式
英文:

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

它的作用是:

  1. 移除所有的存储桶策略
  2. 禁用日志记录
  3. 移除版本
  4. 移除对象

在删除实际存储桶之前。
现在,这段代码片段的含义是:

_, 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 :

  1. remove all bucket policy
  2. disable logging
  3. remove versions and
  4. 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
    }

huangapple
  • 本文由 发表于 2023年1月13日 17:52:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/75107493.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定