forked from ekristen/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paths3-objects.go
136 lines (109 loc) · 2.77 KB
/
s3-objects.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package resources
import (
"context"
"fmt"
"time"
"github.com/gotidy/ptr"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/ekristen/libnuke/pkg/registry"
"github.com/ekristen/libnuke/pkg/resource"
"github.com/ekristen/libnuke/pkg/types"
"github.com/ekristen/aws-nuke/v3/pkg/nuke"
)
const S3ObjectResource = "S3Object"
func init() {
registry.Register(®istry.Registration{
Name: S3ObjectResource,
Scope: nuke.Account,
Lister: &S3ObjectLister{},
})
}
type S3ObjectLister struct{}
func (l *S3ObjectLister) List(_ context.Context, o interface{}) ([]resource.Resource, error) {
opts := o.(*nuke.ListerOpts)
svc := s3.New(opts.Session)
resources := make([]resource.Resource, 0)
buckets, err := DescribeS3Buckets(svc)
if err != nil {
return nil, err
}
for _, bucket := range buckets {
params := &s3.ListObjectVersionsInput{
Bucket: bucket.Name,
}
for {
resp, err := svc.ListObjectVersions(params)
if err != nil {
return nil, err
}
for _, out := range resp.Versions {
if out.Key == nil {
continue
}
resources = append(resources, &S3Object{
svc: svc,
bucket: aws.StringValue(bucket.Name),
creationDate: aws.TimeValue(bucket.CreationDate),
key: *out.Key,
versionID: out.VersionId,
latest: ptr.ToBool(out.IsLatest),
})
}
for _, out := range resp.DeleteMarkers {
if out.Key == nil {
continue
}
resources = append(resources, &S3Object{
svc: svc,
bucket: aws.StringValue(bucket.Name),
creationDate: aws.TimeValue(bucket.CreationDate),
key: *out.Key,
versionID: out.VersionId,
latest: ptr.ToBool(out.IsLatest),
})
}
// make sure to list all with more than 1000 objects
if *resp.IsTruncated {
params.KeyMarker = resp.NextKeyMarker
continue
}
break
}
}
return resources, nil
}
type S3Object struct {
svc *s3.S3
bucket string
creationDate time.Time
key string
versionID *string
latest bool
}
func (e *S3Object) Remove(_ context.Context) error {
params := &s3.DeleteObjectInput{
Bucket: &e.bucket,
Key: &e.key,
VersionId: e.versionID,
}
_, err := e.svc.DeleteObject(params)
if err != nil {
return err
}
return nil
}
func (e *S3Object) Properties() types.Properties {
return types.NewProperties().
Set("Bucket", e.bucket).
Set("Key", e.key).
Set("VersionID", e.versionID).
Set("IsLatest", e.latest).
Set("CreationDate", e.creationDate)
}
func (e *S3Object) String() string {
if e.versionID != nil && *e.versionID != "null" && !e.latest {
return fmt.Sprintf("s3://%s/%s#%s", e.bucket, e.key, *e.versionID)
}
return fmt.Sprintf("s3://%s/%s", e.bucket, e.key)
}