-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathclient.go
73 lines (60 loc) · 1.3 KB
/
client.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package gcs
import (
"context"
"net/http"
"cloud.google.com/go/storage"
"github.com/sirupsen/logrus"
"google.golang.org/api/option"
"gopkg.in/yaml.v2"
"github.com/yunify/qscamel/constants"
"github.com/yunify/qscamel/model"
)
// Client is the client to visit service.
type Client struct {
APIKey string `yaml:"api_key"`
BucketName string `yaml:"bucket_name"`
Path string
client *storage.BucketHandle
}
// New will create a new client.
func New(ctx context.Context, et uint8, hc *http.Client) (c *Client, err error) {
t, err := model.GetTask(ctx)
if err != nil {
return
}
c = &Client{}
e := t.Src
if et == constants.DestinationEndpoint {
e = t.Dst
}
content, err := yaml.Marshal(e.Options)
if err != nil {
return
}
err = yaml.Unmarshal(content, c)
if err != nil {
return
}
// Set bucket name.
if c.BucketName == "" {
logrus.Error("Google cloud storage bucket name can't be empty.")
err = constants.ErrEndpointInvalid
return
}
// Set api key
if c.APIKey == "" {
logrus.Error("Google cloud storage API key can't be empty.")
err = constants.ErrEndpointInvalid
return
}
// Set path.
c.Path = e.Path
svc, err := storage.NewClient(ctx,
option.WithAPIKey(c.APIKey),
option.WithHTTPClient(hc))
if err != nil {
return
}
c.client = svc.Bucket(c.BucketName)
return
}