Skip to content

Commit

Permalink
feat: download file
Browse files Browse the repository at this point in the history
  • Loading branch information
haristhohir committed Dec 4, 2024
1 parent 86f9389 commit e47ca8d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
20 changes: 20 additions & 0 deletions gcs_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type (
ListFiles(bucketName string) ([]string, error)
MoveFile(bucketName, srcObjectName, dstObjectName string) error
DeleteAllFilesInDirectory(bucketName, directory string) error
DownloadFile(bucketName, objectName, destPath string) error
}

gcsManager struct {
Expand Down Expand Up @@ -118,3 +119,22 @@ func (g gcsManager) DeleteAllFilesInDirectory(bucketName, directory string) erro

return nil
}

func (g gcsManager) DownloadFile(bucketName, objectName, destPath string) error {
bucket := g.client.Bucket(bucketName)
obj := bucket.Object(objectName)
r, err := obj.NewReader(g.ctx)
if err != nil {
return err
}
defer r.Close()

out, err := os.Create(destPath)
if err != nil {
return err
}
defer out.Close()

_, err = io.Copy(out, r)
return err
}
14 changes: 14 additions & 0 deletions gcs_manager_mock.go

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

11 changes: 11 additions & 0 deletions gcs_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,14 @@ func TestDeleteAllFilesInDirectory(t *testing.T) {
err := mockGCS.DeleteAllFilesInDirectory("bucketName", "directory/")
assert.NoError(t, err)
}

func TestDownloadFile(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

mockGCS := NewMockGCSManager(ctrl)
mockGCS.EXPECT().DownloadFile("bucketName", "directory/file.ext", "/tmp/").Return(nil)

err := mockGCS.DownloadFile("bucketName", "directory/file.ext", "/tmp/")
assert.NoError(t, err)
}

0 comments on commit e47ca8d

Please sign in to comment.