forked from genevieve/leftovers
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bucket.go
83 lines (67 loc) · 1.53 KB
/
bucket.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
package s3
import (
"fmt"
"github.com/aws/aws-sdk-go/aws/awserr"
awss3 "github.com/aws/aws-sdk-go/service/s3"
)
type Bucket struct {
client bucketsClient
name *string
identifier string
rtype string
}
func NewBucket(client bucketsClient, name *string) Bucket {
return Bucket{
client: client,
name: name,
identifier: *name,
rtype: "S3 Bucket",
}
}
func (b Bucket) Delete() error {
_, err := b.client.DeleteBucket(&awss3.DeleteBucketInput{
Bucket: b.name,
})
if err != nil {
ec2err, ok := err.(awserr.Error)
if ok && ec2err.Code() == "BucketNotEmpty" {
resp, err := b.client.ListObjectVersions(&awss3.ListObjectVersionsInput{Bucket: b.name})
if err != nil {
return err
}
objects := make([]*awss3.ObjectIdentifier, 0)
if len(resp.DeleteMarkers) != 0 {
for _, v := range resp.DeleteMarkers {
objects = append(objects, &awss3.ObjectIdentifier{
Key: v.Key,
VersionId: v.VersionId,
})
}
}
if len(resp.Versions) != 0 {
for _, v := range resp.Versions {
objects = append(objects, &awss3.ObjectIdentifier{
Key: v.Key,
VersionId: v.VersionId,
})
}
}
_, err = b.client.DeleteObjects(&awss3.DeleteObjectsInput{
Bucket: b.name,
Delete: &awss3.Delete{Objects: objects},
})
if err != nil {
return err
}
return b.Delete()
}
return fmt.Errorf("Delete: %s", err)
}
return nil
}
func (b Bucket) Name() string {
return b.identifier
}
func (b Bucket) Type() string {
return b.rtype
}