-
Notifications
You must be signed in to change notification settings - Fork 18
/
storage_rb.go
113 lines (93 loc) · 2.76 KB
/
storage_rb.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 cmd
import (
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/smithy-go"
"github.com/spf13/cobra"
)
var storageRbCmd = &cobra.Command{
Use: "rb sos://BUCKET",
Short: "Delete a bucket",
PreRun: func(cmd *cobra.Command, args []string) {
if len(args) != 1 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], storageBucketPrefix)
},
RunE: func(cmd *cobra.Command, args []string) error {
bucket := args[0]
certsFile, err := cmd.Flags().GetString("certs-file")
if err != nil {
return err
}
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
force, err := cmd.Flags().GetBool("force")
if err != nil {
return err
}
if !force {
if !askQuestion(fmt.Sprintf("Are you sure you want to delete %s%s?", storageBucketPrefix, bucket)) {
return nil
}
}
storage, err := newStorageClient(
storageClientOptWithCertsFile(certsFile),
storageClientOptZoneFromBucket(bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %s", err)
}
if err := storage.deleteBucket(bucket, recursive); err != nil {
return fmt.Errorf("unable to delete bucket: %s", err)
}
if !gQuiet {
fmt.Printf("Bucket %s%s deleted successfully\n", storageBucketPrefix, bucket)
}
return nil
},
}
func init() {
storageRbCmd.Flags().BoolP("force", "f", false, cmdFlagForceHelp)
storageRbCmd.Flags().BoolP("recursive", "r", false,
"empty the bucket before deleting it")
storageCmd.AddCommand(storageRbCmd)
}
func (c storageClient) deleteBucket(bucket string, recursive bool) error {
if recursive {
if _, err := c.deleteObjects(bucket, "", true); err != nil {
return fmt.Errorf("error deleting objects: %s", err)
}
}
// Delete dangling multipart uploads preventing bucket deletion.
res, err := c.ListMultipartUploads(gContext, &s3.ListMultipartUploadsInput{
Bucket: aws.String(bucket),
})
if err != nil {
return fmt.Errorf("error listing dangling multipart uploads: %s", err)
}
for _, mp := range res.Uploads {
if _, err = c.AbortMultipartUpload(gContext, &s3.AbortMultipartUploadInput{
Bucket: aws.String(bucket),
Key: mp.Key,
UploadId: mp.UploadId,
}); err != nil {
return fmt.Errorf("error aborting dangling multipart upload: %s", err)
}
}
if _, err := c.DeleteBucket(gContext, &s3.DeleteBucketInput{Bucket: aws.String(bucket)}); err != nil {
var apiErr smithy.APIError
if errors.As(err, &apiErr) {
if apiErr.ErrorCode() == "BucketNotEmpty" {
return errors.New("bucket is not empty, either delete files before or use flag `-r`")
}
}
return fmt.Errorf("unable to retrieve bucket CORS configuration: %s", err)
}
return nil
}