From 1af9f4061ed83e34cf56cd13425ea860fc3ed6e3 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Fri, 16 Sep 2022 21:25:55 +0800 Subject: [PATCH] fix(s3): remove folder recursively --- drivers/s3/driver.go | 9 +++------ drivers/s3/util.go | 39 ++++++++++++++++++++++++++++++++++----- 2 files changed, 37 insertions(+), 11 deletions(-) diff --git a/drivers/s3/driver.go b/drivers/s3/driver.go index e555fd00e92..28ef5913a63 100644 --- a/drivers/s3/driver.go +++ b/drivers/s3/driver.go @@ -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 { diff --git a/drivers/s3/util.go b/drivers/s3/util.go index 1835dd3b248..e9b76994496 100644 --- a/drivers/s3/util.go +++ b/drivers/s3/util.go @@ -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 } @@ -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 +}