/
gcp.go
144 lines (125 loc) · 4.06 KB
/
gcp.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2023 LiveKit, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package uploader
import (
"context"
"encoding/base64"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
"syscall"
"time"
"cloud.google.com/go/storage"
"github.com/googleapis/gax-go/v2"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
"github.com/livekit/egress/pkg/errors"
"github.com/livekit/egress/pkg/types"
"github.com/livekit/protocol/livekit"
)
const gcpTimeout = time.Minute
type GCPUploader struct {
conf *livekit.GCPUpload
client *storage.Client
}
func newGCPUploader(conf *livekit.GCPUpload) (uploader, error) {
u := &GCPUploader{
conf: conf,
}
var opts []option.ClientOption
if conf.Credentials != "" {
opts = append(opts, option.WithCredentialsJSON([]byte(conf.Credentials)))
}
defaultTransport := http.DefaultTransport.(*http.Transport)
transportClone := defaultTransport.Clone()
// override default transport
defaultTransport.DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return (&net.Dialer{
Timeout: time.Second * 30,
KeepAlive: time.Second * 30,
FallbackDelay: -1,
ControlContext: func(ctx context.Context, network, address string, c syscall.RawConn) error {
// force ipv4 to avoid "service not available in your location, forbidden" errors from Google
if network == "tcp6" {
return errors.New("tcp6 disabled")
}
return nil
},
}).DialContext(ctx, network, addr)
}
if conf.Proxy != nil {
proxyUrl, err := url.Parse(conf.Proxy.Url)
if err != nil {
return nil, err
}
defaultTransport.Proxy = http.ProxyURL(proxyUrl)
if conf.Proxy.Username != "" && conf.Proxy.Password != "" {
auth := fmt.Sprintf("%s:%s", conf.Proxy.Username, conf.Proxy.Password)
basicAuth := "Basic " + base64.StdEncoding.EncodeToString([]byte(auth))
defaultTransport.ProxyConnectHeader = http.Header{}
defaultTransport.ProxyConnectHeader.Add("Proxy-Authorization", basicAuth)
}
}
c, err := storage.NewClient(context.Background(), opts...)
// restore default transport
http.DefaultTransport = transportClone
if err != nil {
return nil, err
}
u.client = c
return u, nil
}
func (u *GCPUploader) upload(localFilepath, storageFilepath string, _ types.OutputType) (string, int64, error) {
file, err := os.Open(localFilepath)
if err != nil {
return "", 0, wrap("GCP", err)
}
defer func() {
_ = file.Close()
}()
stat, err := file.Stat()
if err != nil {
return "", 0, wrap("GCP", err)
}
// In case where the total amount of data to upload is larger than googleapi.DefaultUploadChunkSize, each upload request will have a timeout of
// ChunkRetryDeadline, which is 32s by default. If the request payload is smaller than googleapi.DefaultUploadChunkSize, use a context deadline
// to apply the same timeout
var ctx context.Context
if stat.Size() <= googleapi.DefaultUploadChunkSize {
var cancel context.CancelFunc
ctx, cancel = context.WithTimeout(context.Background(), gcpTimeout)
defer cancel()
} else {
ctx = context.Background()
}
wc := u.client.Bucket(u.conf.Bucket).Object(storageFilepath).Retryer(
storage.WithBackoff(gax.Backoff{
Initial: minDelay,
Max: maxDelay,
Multiplier: 2,
}),
storage.WithPolicy(storage.RetryAlways),
).NewWriter(ctx)
wc.ChunkRetryDeadline = gcpTimeout
if _, err = io.Copy(wc, file); err != nil {
return "", 0, wrap("GCP", err)
}
if err = wc.Close(); err != nil {
return "", 0, wrap("GCP", err)
}
return fmt.Sprintf("https://%s.storage.googleapis.com/%s", u.conf.Bucket, storageFilepath), stat.Size(), nil
}