From e1fbb2e98e9279c4eaf4818b9992fa4007c25f43 Mon Sep 17 00:00:00 2001 From: ryo nakamaru Date: Tue, 4 Jul 2017 23:35:53 +0900 Subject: [PATCH] list files just only those prefix matched the specified key --- main.go | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/main.go b/main.go index f1e8e06..f345831 100644 --- a/main.go +++ b/main.go @@ -319,8 +319,8 @@ func s3get(backet, key string) (*s3.GetObjectOutput, error) { } func s3listFiles(w http.ResponseWriter, r *http.Request, backet, key string) { - if strings.HasSuffix(key, "/") { - key = key[:len(key)-1] + if strings.HasPrefix(key, "/") { + key = key[1:] } req := &s3.ListObjectsInput{ Bucket: aws.String(backet), @@ -334,7 +334,14 @@ func s3listFiles(w http.ResponseWriter, r *http.Request, backet, key string) { } files := []string{} for _, obj := range result.Contents { - files = append(files, strings.Replace(aws.StringValue(obj.Key), key, "", -1)) + candidate := strings.Replace(aws.StringValue(obj.Key), key, "", -1) + if len(candidate) == 0 { + continue + } + if strings.Contains(candidate, "/") { + continue + } + files = append(files, candidate) } bytes, merr := json.Marshal(files) if merr != nil {