Skip to content

Commit

Permalink
feat: get file list filtered by prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
haristhohir committed Dec 4, 2024
1 parent e47ca8d commit a89c348
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 9 deletions.
12 changes: 9 additions & 3 deletions gcs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ type (
GCSManager interface {
UploadFile(bucketName, objectName string, file os.File) error
DeleteFile(bucketName, objectName string) error
ListFiles(bucketName string) ([]string, error)
ListFiles(bucketName, directory string) ([]string, error)
MoveFile(bucketName, srcObjectName, dstObjectName string) error
DeleteAllFilesInDirectory(bucketName, directory string) error
DownloadFile(bucketName, objectName, destPath string) error
Expand Down Expand Up @@ -63,9 +63,15 @@ func (g gcsManager) DeleteFile(bucketName, objectName string) error {
return nil
}

func (g gcsManager) ListFiles(bucketName string) ([]string, error) {
func (g gcsManager) ListFiles(bucketName, directory string) ([]string, error) {
if directory != "" && directory[len(directory)-1] != '/' {
directory += "/"
}

var files []string
it := g.client.Bucket(bucketName).Objects(g.ctx, nil)
it := g.client.Bucket(bucketName).Objects(g.ctx, &storage.Query{
Prefix: directory,
})
for {
attrs, err := it.Next()
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions gcs_manager_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions gcs_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ func TestListFiles(t *testing.T) {

mockGCS := NewMockGCSManager(ctrl)
expectedFiles := []string{"file1", "file2"}
mockGCS.EXPECT().ListFiles("bucketName").Return(expectedFiles, nil)
mockGCS.EXPECT().ListFiles("bucketName", "/").Return(expectedFiles, nil)

files, err := mockGCS.ListFiles("bucketName")
files, err := mockGCS.ListFiles("bucketName", "/")
assert.NoError(t, err)
assert.Equal(t, expectedFiles, files)
}
Expand Down

0 comments on commit a89c348

Please sign in to comment.