-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
45 lines (39 loc) · 1.05 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
package gcsext
import (
"context"
"fmt"
"net/http"
"time"
"cloud.google.com/go/storage"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
ghttp "google.golang.org/api/transport/http"
)
func NewClient(
ctx context.Context,
baseTransport http.RoundTripper,
scopes ...string,
) (*storage.Client, error) {
ctx = context.WithValue(ctx, oauth2.HTTPClient, &http.Client{
Timeout: 20 * time.Second,
Transport: baseTransport,
})
credentials, err := google.FindDefaultCredentials(ctx, scopes...)
if err != nil {
return nil, fmt.Errorf("find default google credentials: %w", err)
}
transport, err := ghttp.NewTransport(ctx, baseTransport, option.WithTokenSource(credentials.TokenSource))
if err != nil {
return nil, fmt.Errorf("new transport: %w", err)
}
httpClient := &http.Client{
Timeout: 20 * time.Second,
Transport: transport,
}
client, err := storage.NewClient(ctx, option.WithHTTPClient(httpClient))
if err != nil {
return nil, fmt.Errorf("new storage client: %w", err)
}
return client, nil
}