forked from tencentyun/coscli
/
abort.go
55 lines (44 loc) · 1.41 KB
/
abort.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
package cmd
import (
"context"
"coscli/util"
logger "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
var abortCmd = &cobra.Command{
Use: "abort",
Short: "Abort parts",
Long: `Abort parts
Format:
./coscli abort cos://<bucket-name>[/<prefix>] [flags]
Example:
./coscli abort cos://examplebucket/test/`,
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
include, _ := cmd.Flags().GetString("include")
exclude, _ := cmd.Flags().GetString("exclude")
abortParts(args[0], include, exclude)
},
}
func init() {
rootCmd.AddCommand(abortCmd)
abortCmd.Flags().String("include", "", "List files that meet the specified criteria")
abortCmd.Flags().String("exclude", "", "Exclude files that meet the specified criteria")
}
func abortParts(arg string, include string, exclude string) {
bucketName, cosPath := util.ParsePath(arg)
c := util.NewClient(&config, ¶m, bucketName)
uploads := util.GetUploadsListRecursive(c, cosPath, 0, include, exclude)
successCnt, failCnt := 0, 0
for _, u := range uploads {
_, err := c.Object.AbortMultipartUpload(context.Background(), u.Key, u.UploadID)
if err != nil {
logger.Infoln("Abort fail! UploadID:", u.UploadID, "Key:", u.Key)
failCnt++
} else {
logger.Infoln("Abort success! UploadID:", u.UploadID, "Key:", u.Key)
successCnt++
}
}
logger.Infoln("Total:", len(uploads), ",", successCnt, "Success,", failCnt, "Fail")
}