Skip to content
This repository was archived by the owner on Oct 15, 2024. It is now read-only.

Commit d05ed74

Browse files
authored
fix s3 buckets (#773)
1 parent f8acfca commit d05ed74

File tree

3 files changed

+49
-40
lines changed

3 files changed

+49
-40
lines changed

resources/s3-buckets.go

+22-19
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package resources
22

33
import (
44
"fmt"
5+
"time"
56

67
"github.com/aws/aws-sdk-go/aws"
78
"github.com/aws/aws-sdk-go/aws/awserr"
@@ -20,9 +21,10 @@ func init() {
2021
}
2122

2223
type S3Bucket struct {
23-
svc *s3.S3
24-
name string
25-
tags []*s3.Tag
24+
svc *s3.S3
25+
name string
26+
creationDate time.Time
27+
tags []*s3.Tag
2628
}
2729

2830
func ListS3Buckets(s *session.Session) ([]Resource, error) {
@@ -34,41 +36,43 @@ func ListS3Buckets(s *session.Session) ([]Resource, error) {
3436
}
3537

3638
resources := make([]Resource, 0)
37-
for _, name := range buckets {
39+
for _, bucket := range buckets {
3840
tags, err := svc.GetBucketTagging(&s3.GetBucketTaggingInput{
39-
Bucket: aws.String(name),
41+
Bucket: bucket.Name,
4042
})
4143

4244
if err != nil {
4345
if aerr, ok := err.(awserr.Error); ok {
4446
if aerr.Code() == "NoSuchTagSet" {
4547
resources = append(resources, &S3Bucket{
46-
svc: svc,
47-
name: name,
48-
tags: make([]*s3.Tag, 0),
48+
svc: svc,
49+
name: aws.StringValue(bucket.Name),
50+
creationDate: aws.TimeValue(bucket.CreationDate),
51+
tags: make([]*s3.Tag, 0),
4952
})
5053
}
5154
}
5255
continue
5356
}
5457

5558
resources = append(resources, &S3Bucket{
56-
svc: svc,
57-
name: name,
58-
tags: tags.TagSet,
59+
svc: svc,
60+
name: aws.StringValue(bucket.Name),
61+
creationDate: aws.TimeValue(bucket.CreationDate),
62+
tags: tags.TagSet,
5963
})
6064
}
6165

6266
return resources, nil
6367
}
6468

65-
func DescribeS3Buckets(svc *s3.S3) ([]string, error) {
69+
func DescribeS3Buckets(svc *s3.S3) ([]s3.Bucket, error) {
6670
resp, err := svc.ListBuckets(nil)
6771
if err != nil {
6872
return nil, err
6973
}
7074

71-
buckets := make([]string, 0)
75+
buckets := make([]s3.Bucket, 0)
7276
for _, out := range resp.Buckets {
7377
bucketLocationResponse, err := svc.GetBucketLocation(&s3.GetBucketLocationInput{Bucket: out.Name})
7478

@@ -78,10 +82,9 @@ func DescribeS3Buckets(svc *s3.S3) ([]string, error) {
7882

7983
location := UnPtrString(bucketLocationResponse.LocationConstraint, endpoints.UsEast1RegionID)
8084
region := UnPtrString(svc.Config.Region, endpoints.UsEast1RegionID)
81-
if location == region {
82-
buckets = append(buckets, *out.Name)
85+
if location == region && out != nil {
86+
buckets = append(buckets, *out)
8387
}
84-
8588
}
8689

8790
return buckets, nil
@@ -139,9 +142,9 @@ func (e *S3Bucket) RemoveAllObjects() error {
139142
}
140143

141144
func (e *S3Bucket) Properties() types.Properties {
142-
properties := types.NewProperties()
143-
properties.Set("Name", e.name).
144-
properties.Set("CreationDate", e.CreationDate)
145+
properties := types.NewProperties().
146+
Set("Name", e.name).
147+
Set("CreationDate", e.creationDate)
145148

146149
for _, tag := range e.tags {
147150
properties.SetTag(tag.Key, tag.Value)

resources/s3-multipart-uploads.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package resources
33
import (
44
"fmt"
55

6+
"github.com/aws/aws-sdk-go/aws"
67
"github.com/aws/aws-sdk-go/aws/session"
78
"github.com/aws/aws-sdk-go/service/s3"
89
"github.com/rebuy-de/aws-nuke/pkg/types"
@@ -29,9 +30,9 @@ func ListS3MultipartUpload(sess *session.Session) ([]Resource, error) {
2930
return nil, err
3031
}
3132

32-
for _, name := range buckets {
33+
for _, bucket := range buckets {
3334
params := &s3.ListMultipartUploadsInput{
34-
Bucket: &name,
35+
Bucket: bucket.Name,
3536
}
3637

3738
for {
@@ -47,7 +48,7 @@ func ListS3MultipartUpload(sess *session.Session) ([]Resource, error) {
4748

4849
resources = append(resources, &S3MultipartUpload{
4950
svc: svc,
50-
bucket: name,
51+
bucket: aws.StringValue(bucket.Name),
5152
key: *upload.Key,
5253
uploadID: *upload.UploadId,
5354
})

resources/s3-objects.go

+23-18
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,21 @@ package resources
22

33
import (
44
"fmt"
5+
"time"
56

7+
"github.com/aws/aws-sdk-go/aws"
68
"github.com/aws/aws-sdk-go/aws/session"
79
"github.com/aws/aws-sdk-go/service/s3"
810
"github.com/rebuy-de/aws-nuke/pkg/types"
911
)
1012

1113
type S3Object struct {
12-
svc *s3.S3
13-
bucket string
14-
key string
15-
versionID *string
16-
latest bool
14+
svc *s3.S3
15+
bucket string
16+
creationDate time.Time
17+
key string
18+
versionID *string
19+
latest bool
1720
}
1821

1922
func init() {
@@ -30,9 +33,9 @@ func ListS3Objects(sess *session.Session) ([]Resource, error) {
3033
return nil, err
3134
}
3235

33-
for _, name := range buckets {
36+
for _, bucket := range buckets {
3437
params := &s3.ListObjectVersionsInput{
35-
Bucket: &name,
38+
Bucket: bucket.Name,
3639
}
3740

3841
for {
@@ -47,11 +50,12 @@ func ListS3Objects(sess *session.Session) ([]Resource, error) {
4750
}
4851

4952
resources = append(resources, &S3Object{
50-
svc: svc,
51-
bucket: name,
52-
key: *out.Key,
53-
versionID: out.VersionId,
54-
latest: UnPtrBool(out.IsLatest, false),
53+
svc: svc,
54+
bucket: aws.StringValue(bucket.Name),
55+
creationDate: aws.TimeValue(bucket.CreationDate),
56+
key: *out.Key,
57+
versionID: out.VersionId,
58+
latest: UnPtrBool(out.IsLatest, false),
5559
})
5660
}
5761

@@ -61,11 +65,12 @@ func ListS3Objects(sess *session.Session) ([]Resource, error) {
6165
}
6266

6367
resources = append(resources, &S3Object{
64-
svc: svc,
65-
bucket: name,
66-
key: *out.Key,
67-
versionID: out.VersionId,
68-
latest: UnPtrBool(out.IsLatest, false),
68+
svc: svc,
69+
bucket: aws.StringValue(bucket.Name),
70+
creationDate: aws.TimeValue(bucket.CreationDate),
71+
key: *out.Key,
72+
versionID: out.VersionId,
73+
latest: UnPtrBool(out.IsLatest, false),
6974
})
7075
}
7176

@@ -103,7 +108,7 @@ func (e *S3Object) Properties() types.Properties {
103108
Set("Key", e.key).
104109
Set("VersionID", e.versionID).
105110
Set("IsLatest", e.latest).
106-
Set("CreationDate", e.CreationDate)
111+
Set("CreationDate", e.creationDate)
107112
}
108113

109114
func (e *S3Object) String() string {

0 commit comments

Comments
 (0)