Skip to content

Commit

Permalink
Bug Fix: Fix the bug that will fail when the number of deleted object…
Browse files Browse the repository at this point in the history
…s is greater than 1000
  • Loading branch information
ChloroplastYu committed Nov 14, 2021
1 parent 0f23daa commit 179519e
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 1 deletion.
71 changes: 70 additions & 1 deletion cmd/rm.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ Example:
include, _ := cmd.Flags().GetString("include")
exclude, _ := cmd.Flags().GetString("exclude")
if recursive {
removeObjects(args, include, exclude, force)
removeObjects1(args, include, exclude, force)
} else {
removeObject(args, force)
}
Expand Down Expand Up @@ -109,6 +109,75 @@ func removeObjects(args []string, include string, exclude string, force bool) {
}
}

func removeObjects1(args []string, include string, exclude string, force bool) {
for _, arg := range args {
bucketName, cosDir := util.ParsePath(arg)
c := util.NewClient(&config, bucketName)

if cosDir != "" && (cosDir[len(cosDir)-1] != '/' || cosDir[len(cosDir)-1] != '\\') {
cosDir += "/"
}

isTruncated := true
nextMarker := ""
deleteOrNot := false
errorOrNot := false
for isTruncated {
objects, t, m:= util.GetObjectsListIterator(c, cosDir, nextMarker, include, exclude)
isTruncated = t
nextMarker = m

var oKeys []cos.Object
for _, o := range objects {
if !force {
fmt.Printf("Do you want to delete %s? (y/n)", o.Key)
var choice string
_, _ = fmt.Scanf("%s\n", &choice)
if choice == "" || choice == "y" || choice == "Y" || choice == "yes" || choice == "Yes" || choice == "YES" {
oKeys = append(oKeys, cos.Object{Key: o.Key})
}
} else {
oKeys = append(oKeys, cos.Object{Key: o.Key})
}
}
if len(oKeys) > 0 {
deleteOrNot = true
}

opt := &cos.ObjectDeleteMultiOptions{
XMLName: xml.Name{},
Quiet: false,
Objects: oKeys,
}

res, _, err := c.Object.DeleteMulti(context.Background(), opt)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

for _, o := range res.DeletedObjects {
fmt.Println("Delete ", o.Key)
}
if len(res.Errors) > 0 {
errorOrNot = true
fmt.Println()
for _, e := range res.Errors {
fmt.Println("Fail to delete", e.Key)
fmt.Println(" Error Code: ", e.Code, " Message: ", e.Message)
}
}
}

if deleteOrNot == false {
fmt.Println("No objects were deleted!")
}
if errorOrNot == false {
fmt.Printf("\nAll deleted successfully!\n")
}
}
}

func removeObject(args []string, force bool) {
for _, arg := range args {
bucketName, cosPath := util.ParsePath(arg)
Expand Down
38 changes: 38 additions & 0 deletions util/list_iterator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package util

import (
"context"
"fmt"
"github.com/tencentyun/cos-go-sdk-v5"
"os"
)

func GetObjectsListIterator(c *cos.Client, prefix, marker string, include, exclude string) (objects []cos.Object, isTruncated bool, nextMarker string) {
opt := &cos.BucketGetOptions{
Prefix: prefix,
Delimiter: "",
EncodingType: "",
Marker: marker,
MaxKeys: 0,
}


res, _, err := c.Bucket.Get(context.Background(), opt)
if err != nil {
_, _ = fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}

objects = append(objects, res.Contents...)
isTruncated = res.IsTruncated
nextMarker = res.NextMarker

if len(include) > 0 {
objects = MatchCosPattern(objects, include, true)
}
if len(exclude) > 0 {
objects = MatchCosPattern(objects, exclude, false)
}

return objects, isTruncated, nextMarker
}

0 comments on commit 179519e

Please sign in to comment.