-
Notifications
You must be signed in to change notification settings - Fork 0
/
StorageClient.go
48 lines (39 loc) · 1.2 KB
/
StorageClient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
package gcp
import (
"cloud.google.com/go/storage"
"context"
"github.com/helstern/kommol/internal/bootstrap/config"
"github.com/helstern/kommol/internal/infrastructure/di/builder"
"github.com/sarulabs/di/v2"
"google.golang.org/api/option"
)
type StorageClient struct {
key string
}
func (o *StorageClient) Get(ctn di.Container) *storage.Client {
return ctn.Get(o.key).(*storage.Client)
}
func (o *StorageClient) Module(ctx context.Context, builder *di.Builder) error {
return builder.Add(di.Def{
Name: o.key,
Build: func(ctn di.Container) (interface{}, error) {
appConfig := config.Get(ctn)
credentials := appConfig.GCP.GetCredentialFilePath()
if credentials != "" {
return storage.NewClient(ctx, option.WithCredentialsFile(credentials), option.WithScopes("https://www.googleapis.com/auth/devstorage.read_only"))
}
return storage.NewClient(ctx, option.WithScopes("https://www.googleapis.com/auth/devstorage.read_only"))
},
})
}
var (
bootstrapStorageClient *StorageClient
)
func GetStorageClient() *StorageClient {
if bootstrapStorageClient == nil {
bootstrapStorageClient = &StorageClient{
key: builder.TypeName((*storage.Client)(nil)),
}
}
return bootstrapStorageClient
}