Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix listobjects_v2.go logic of extracting key #16

Merged
merged 1 commit into from
Jul 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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(""), "")
}