Skip to content

Commit

Permalink
list files just only those prefix matched the specified key
Browse files Browse the repository at this point in the history
  • Loading branch information
pottava committed Jul 4, 2017
1 parent 8098b45 commit e1fbb2e
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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 {
Expand Down

0 comments on commit e1fbb2e

Please sign in to comment.