-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgce.go
57 lines (47 loc) · 1.27 KB
/
gce.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
49
50
51
52
53
54
55
56
57
package storage
import (
"context"
"fmt"
"io"
"os"
"path"
"cloud.google.com/go/storage"
"github.com/mshogin/randomtrader/pkg/config"
"google.golang.org/api/option"
)
type gceClientImpl struct {
cli *storage.Client
}
var gceClient *gceClientImpl
// GetGCEClient ...
var GetGCEClient = func() (Storage, error) {
if gceClient != nil {
return gceClient, nil
}
ctx := context.Background()
cli, err := storage.NewClient(ctx, option.WithCredentialsFile(config.GetGCEServiceKeyFilepath()))
if err != nil {
return nil, fmt.Errorf("cannot create gce storage client: %w", err)
}
gceClient = &gceClientImpl{cli: cli}
return gceClient, nil
}
// SaveObject ...
func (m *gceClientImpl) SaveObject(prefix, fpath string) error {
f, err := os.Open(fpath)
if err != nil {
return fmt.Errorf("cannot open file %q: %w", fpath, err)
}
defer f.Close()
ctx, cancel := context.WithTimeout(context.Background(), saveObjectTimeout)
defer cancel()
_, objectName := path.Split(fpath)
wc := m.cli.Bucket(config.GetGCEBucket()).Object(prefix + objectName).NewWriter(ctx)
if _, err = io.Copy(wc, f); err != nil {
return fmt.Errorf("cannot copy file to the bucket: %w", err)
}
if err := wc.Close(); err != nil {
return fmt.Errorf("cannot close remote object: %v", err)
}
return nil
}