From c617f7349d886b8d236c2887fbe6785486f2867c Mon Sep 17 00:00:00 2001 From: TOL Date: Thu, 18 Jul 2024 07:41:01 +0900 Subject: [PATCH] Fix listobjects_v2.go logic of extracting key (#16) --- core/amazon/awssdk/listobjects_v2.go | 12 +++++++++++- core/amazon/awssdk/listobjects_v2_test.go | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 4 deletions(-) diff --git a/core/amazon/awssdk/listobjects_v2.go b/core/amazon/awssdk/listobjects_v2.go index ac05147..0a7c737 100644 --- a/core/amazon/awssdk/listobjects_v2.go +++ b/core/amazon/awssdk/listobjects_v2.go @@ -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(), }) } @@ -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:], "/") +} diff --git a/core/amazon/awssdk/listobjects_v2_test.go b/core/amazon/awssdk/listobjects_v2_test.go index f66a859..f232b80 100644 --- a/core/amazon/awssdk/listobjects_v2_test.go +++ b/core/amazon/awssdk/listobjects_v2_test.go @@ -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) } @@ -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(""), "") +}