Skip to content

Commit

Permalink
Fix listobjects_v2.go logic of extracting key (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
tttol authored Jul 17, 2024
1 parent 6b97e84 commit c617f73
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
12 changes: 11 additions & 1 deletion core/amazon/awssdk/listobjects_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func ListObjects(rootDir string, uploadDirName string) ([]Item, error) {
}
if !info.IsDir() {
items = append(items, Item{
Key: filepath.ToSlash(path[len(uploadDirName+"/"):]),
Key: ExtractKey(path),
Size: info.Size(),
})
}
Expand All @@ -85,3 +85,13 @@ func IsTruncated(items []Item) (bool, []Item) {
return false, items
}
}

func ExtractKey(path string) string {
splitted := strings.Split(path, "/")
if len(splitted) < 3 {
slog.Warn("Failed to extract key. Unexpected number of slash in path.", "path", path)
return ""
}

return strings.Join(splitted[2:], "/")
}
15 changes: 12 additions & 3 deletions core/amazon/awssdk/listobjects_v2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ func TestListObjects(t *testing.T) {
}
defer os.RemoveAll(tempDir)

err = os.MkdirAll(filepath.Join(tempDir, "dir1"), os.ModePerm)
err = os.MkdirAll(filepath.Join(tempDir, "test-bucket", "dir1"), os.ModePerm)
if err != nil {
t.Fatal(err)
}

err = os.WriteFile(filepath.Join(tempDir, "file1.txt"), []byte("file1 content"), os.ModePerm)
err = os.WriteFile(filepath.Join(tempDir, "test-bucket", "file1.txt"), []byte("file1 content"), os.ModePerm)
if err != nil {
t.Fatal(err)
}

err = os.WriteFile(filepath.Join(tempDir, "dir1", "file2.txt"), []byte("file2 content"), os.ModePerm)
err = os.WriteFile(filepath.Join(tempDir, "test-bucket", "dir1", "file2.txt"), []byte("file2 content"), os.ModePerm)
if err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -80,3 +80,12 @@ func TestIsTruncated(t *testing.T) {
assert.Equal(t, actualItems1200, items1200[:1000])

}

func TestExtractKey(t *testing.T) {
assert.Equal(t, ExtractKey("upload/test-bucket/aaa.txt"), "aaa.txt")
assert.Equal(t, ExtractKey("upload/test-bucket/dir1/aaa.txt"), "dir1/aaa.txt")
assert.Equal(t, ExtractKey("upload/test-bucket/dir1/dir2/dir3/dir4/aaa.txt"), "dir1/dir2/dir3/dir4/aaa.txt")
assert.Equal(t, ExtractKey("upload/aaa.txt"), "")
assert.Equal(t, ExtractKey("aaa.txt"), "")
assert.Equal(t, ExtractKey(""), "")
}

0 comments on commit c617f73

Please sign in to comment.