Skip to content

Commit

Permalink
feat<google> Add New Cloud Storage Service Functions
Browse files Browse the repository at this point in the history
*  Add ListBucket() function
*  Add CloseClient() function
  • Loading branch information
mushoffa committed Mar 22, 2022
1 parent 4174ed6 commit ff0f651
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 2 deletions.
39 changes: 37 additions & 2 deletions google/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -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()

Expand All @@ -98,4 +129,8 @@ func (g *gcstorage) DownloadFile(bucketName, fileName string) ([]byte, error) {
}

return dataFile, nil
}

func (g *gcstorage) CloseClient() {
g.client.Close()
}
15 changes: 15 additions & 0 deletions google/storage_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package google

import (
"log"
"testing"

"github.com/mushoffa/go-library/google"
Expand All @@ -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)
}
}

0 comments on commit ff0f651

Please sign in to comment.