forked from rebuy-de/aws-nuke
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3-objects.go
113 lines (92 loc) · 2.1 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
package resources
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/rebuy-de/aws-nuke/pkg/types"
)
type S3Object struct {
svc *s3.S3
bucket string
key string
versionID *string
latest bool
}
func init() {
register("S3Object", ListS3Objects)
}
func ListS3Objects(sess *session.Session) ([]Resource, error) {
svc := s3.New(sess)
resources := make([]Resource, 0)
buckets, err := DescribeS3Buckets(svc)
if err != nil {
return nil, err
}
for _, name := range buckets {
params := &s3.ListObjectVersionsInput{
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: name,
key: *out.Key,
versionID: out.VersionId,
latest: UnPtrBool(out.IsLatest, false),
})
}
for _, out := range resp.DeleteMarkers {
if out.Key == nil {
continue
}
resources = append(resources, &S3Object{
svc: svc,
bucket: name,
key: *out.Key,
versionID: out.VersionId,
latest: UnPtrBool(out.IsLatest, false),
})
}
// make sure to list all with more than 1000 objects
if *resp.IsTruncated {
params.KeyMarker = resp.NextKeyMarker
continue
}
break
}
}
return resources, nil
}
func (e *S3Object) Remove() 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)
}
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)
}