Skip to content

Commit

Permalink
fix(s3): remove folder recursively
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Sep 16, 2022
1 parent 0d012f8 commit 1af9f40
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
9 changes: 3 additions & 6 deletions drivers/s3/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,10 @@ func (d *S3) Copy(ctx context.Context, srcObj, dstDir model.Obj) error {
}

func (d *S3) Remove(ctx context.Context, obj model.Obj) error {
key := getKey(obj.GetPath(), obj.IsDir())
input := &s3.DeleteObjectInput{
Bucket: &d.Bucket,
Key: &key,
if obj.IsDir() {
return d.removeDir(ctx, obj.GetPath())
}
_, err := d.client.DeleteObject(input)
return err
return d.removeFile(obj.GetPath())
}

func (d *S3) Put(ctx context.Context, dstDir model.Obj, stream model.FileStreamer, up driver.UpdateProgress) error {
Expand Down
39 changes: 34 additions & 5 deletions drivers/s3/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ func getKey(path string, dir bool) string {
return path
}

var defaultPlaceholderName = ".placeholder"

// var defaultPlaceholderName = ".placeholder"
func getPlaceholderName(placeholder string) string {
if placeholder == "" {
return defaultPlaceholderName
}
//if placeholder == "" {
// return defaultPlaceholderName
//}
return placeholder
}

Expand Down Expand Up @@ -205,3 +204,33 @@ func (d *S3) copyDir(ctx context.Context, src string, dst string) error {
}
return nil
}

func (d *S3) removeDir(ctx context.Context, src string) error {
objs, err := op.List(ctx, d, src, model.ListArgs{})
if err != nil {
return err
}
for _, obj := range objs {
cSrc := path.Join(src, obj.GetName())
if obj.IsDir() {
err = d.removeDir(ctx, cSrc)
} else {
err = d.removeFile(cSrc)
}
if err != nil {
return err
}
}
_ = d.removeFile(path.Join(src, getPlaceholderName(d.Placeholder)))
return nil
}

func (d *S3) removeFile(src string) error {
key := getKey(src, true)
input := &s3.DeleteObjectInput{
Bucket: &d.Bucket,
Key: &key,
}
_, err := d.client.DeleteObject(input)
return err
}

0 comments on commit 1af9f40

Please sign in to comment.