From ff0f651c4d88319cfb48e4dbe724d98f17646900 Mon Sep 17 00:00:00 2001 From: mushoffa Date: Tue, 22 Mar 2022 11:53:00 +0700 Subject: [PATCH] feat Add New Cloud Storage Service Functions * Add ListBucket() function * Add CloseClient() function --- google/storage.go | 39 +++++++++++++++++++++++++++++++++++++-- google/storage_test.go | 15 +++++++++++++++ 2 files changed, 52 insertions(+), 2 deletions(-) diff --git a/google/storage.go b/google/storage.go index b2ab8be..615f66e 100644 --- a/google/storage.go +++ b/google/storage.go @@ -10,13 +10,16 @@ import ( "time" "cloud.google.com/go/storage" + "google.golang.org/api/iterator" ) type CloudStorageService interface { GetInstance() *storage.Client CreateBucket(string) error + ListBucket() ([]string, error) UploadFile(multipart.File, string, string, string) error DownloadFile(string, string) ([]byte, error) + CloseClient() } type gcstorage struct { @@ -31,15 +34,15 @@ func NewCloudStorageClient(projectID string) (CloudStorageService, error) { log.Fatalf("Failed to create client: %v", err) } - defer client.Close() - return &gcstorage{client, projectID}, nil } +// GetInstance ... func (g *gcstorage) GetInstance() *storage.Client { return g.client } +// CreateBucket ... func (g *gcstorage) CreateBucket(bucketName string) error { ctx := context.Background() @@ -57,6 +60,33 @@ func (g *gcstorage) CreateBucket(bucketName string) error { return nil } +// ListBucket ... +func (g *gcstorage) ListBucket() ([]string, error) { + ctx := context.Background() + + ctx, cancel := context.WithTimeout(ctx, time.Second*10) + defer cancel() + + var buckets []string + it := g.client.Buckets(ctx, g.id) + for { + battrs, err := it.Next() + if err == iterator.Done { + break + } + + if err != nil { + return nil, err + } + + buckets = append(buckets, battrs.Name) + } + + return buckets, nil +} + + +// UploadFile ... func (g *gcstorage) UploadFile(file multipart.File, bucketName, folderName, fileName string) error { ctx := context.Background() @@ -79,6 +109,7 @@ func (g *gcstorage) UploadFile(file multipart.File, bucketName, folderName, file return nil } +// DownloadFile ... func (g *gcstorage) DownloadFile(bucketName, fileName string) ([]byte, error) { ctx := context.Background() @@ -98,4 +129,8 @@ func (g *gcstorage) DownloadFile(bucketName, fileName string) ([]byte, error) { } return dataFile, nil +} + +func (g *gcstorage) CloseClient() { + g.client.Close() } \ No newline at end of file diff --git a/google/storage_test.go b/google/storage_test.go index 590eba1..b5e46df 100644 --- a/google/storage_test.go +++ b/google/storage_test.go @@ -1,6 +1,7 @@ package google import ( + "log" "testing" "github.com/mushoffa/go-library/google" @@ -15,4 +16,18 @@ func TestNewCloudStorageClient_Success(t *testing.T) { if gcstorage == nil { t.Errorf("Invalid cloud storage client: %v", gcstorage) } +} + +func TestListBucket_Success(t *testing.T) { + gcstorage, err := google.NewCloudStorageClient("YOUR_PROJECT_ID") + if err != nil { + t.Errorf("Error creating cloud storage client: %v", err) + } + + buckets, err := gcstorage.ListBucket() + if err != nil { + t.Errorf("Error getting buckets: %v", err) + } else { + log.Println(buckets) + } } \ No newline at end of file