-
Notifications
You must be signed in to change notification settings - Fork 20
/
storage_metadata_delete.go
110 lines (86 loc) · 2.72 KB
/
storage_metadata_delete.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
package cmd
import (
"fmt"
"strings"
"github.com/aws/aws-sdk-go-v2/aws"
s3types "github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/spf13/cobra"
)
var storageMetadataDeleteCmd = &cobra.Command{
Use: "delete sos://BUCKET/(OBJECT|PREFIX/) KEY...",
Aliases: []string{"del"},
Short: "Delete metadata from an object",
Long: fmt.Sprintf(`This command deletes key/value metadata from an object.
Example:
exo storage metadata delete sos://my-bucket/object-a k1
Supported output template annotations: %s`,
strings.Join(outputterTemplateAnnotations(&storageShowObjectOutput{}), ", ")),
PreRunE: func(cmd *cobra.Command, args []string) error {
if len(args) < 2 {
cmdExitOnUsageError(cmd, "invalid arguments")
}
args[0] = strings.TrimPrefix(args[0], storageBucketPrefix)
if !strings.Contains(args[0], "/") {
cmdExitOnUsageError(cmd, fmt.Sprintf("invalid argument: %q", args[0]))
}
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
var (
bucket string
prefix string
)
certsFile, err := cmd.Flags().GetString("certs-file")
if err != nil {
return err
}
recursive, err := cmd.Flags().GetBool("recursive")
if err != nil {
return err
}
parts := strings.SplitN(args[0], "/", 2)
bucket, prefix = parts[0], parts[1]
mdKeys := args[1:]
storage, err := newStorageClient(
storageClientOptWithCertsFile(certsFile),
storageClientOptZoneFromBucket(bucket),
)
if err != nil {
return fmt.Errorf("unable to initialize storage client: %v", err)
}
if err := storage.deleteObjectsMetadata(bucket, prefix, mdKeys, recursive); err != nil {
return fmt.Errorf("unable to delete metadata from object: %s", err)
}
if !gQuiet && !recursive && !strings.HasSuffix(prefix, "/") {
return output(storage.showObject(bucket, prefix))
}
if !gQuiet {
fmt.Println("Metadata deleted successfully")
}
return nil
},
}
func init() {
storageMetadataDeleteCmd.Flags().BoolP("recursive", "r", false,
"delete metadata recursively (with object prefix only)")
storageMetadataCmd.AddCommand(storageMetadataDeleteCmd)
}
func (c *storageClient) deleteObjectMetadata(bucket, key string, mdKeys []string) error {
object, err := c.copyObject(bucket, key)
if err != nil {
return err
}
for _, k := range mdKeys {
if _, ok := object.Metadata[k]; !ok {
return fmt.Errorf("key %q not found in current metadata", k)
}
delete(object.Metadata, k)
}
_, err = c.CopyObject(gContext, object)
return err
}
func (c *storageClient) deleteObjectsMetadata(bucket, prefix string, mdKeys []string, recursive bool) error {
return c.forEachObject(bucket, prefix, recursive, func(o *s3types.Object) error {
return c.deleteObjectMetadata(bucket, aws.ToString(o.Key), mdKeys)
})
}