From 42b5043411fbbbef9c750f0b13bbea9c9acb9055 Mon Sep 17 00:00:00 2001 From: brxie Date: Fri, 7 Feb 2020 15:11:08 +0100 Subject: [PATCH] fix (s3): fix remove a single object (#225) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Objects, as well as buckets are removed basing on objecs list received from client. As the objects are fetched by Prefix, the request for removing object 'foo' will remove all 'foo*' objects in this bucket. For instance, having bucket with objects like so: awesomebucket/ ├── foo ├── foobar └── thing Rm("awesomebucket/foo") will have effect: awesomebucket/ └── thing This change fixes this bug by recognizing if single object has to be removed or the entire bucket. For single object, we don't need to walk through directories and can request to remove directly. --- server/model/backend/s3.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/server/model/backend/s3.go b/server/model/backend/s3.go index 7d487ef3..24399913 100644 --- a/server/model/backend/s3.go +++ b/server/model/backend/s3.go @@ -226,6 +226,14 @@ func (s S3Backend) Rm(path string) error { return NewError("Doesn't exist", 404) } + if !strings.HasSuffix(p.path, "/") { + _, err := client.DeleteObject(&s3.DeleteObjectInput{ + Bucket: aws.String(p.bucket), + Key: &p.path, + }) + return err + } + objs, err := client.ListObjects(&s3.ListObjectsInput{ Bucket: aws.String(p.bucket), Prefix: aws.String(p.path),