Skip to content

Commit

Permalink
simplify
Browse files Browse the repository at this point in the history
  • Loading branch information
frostbyte73 committed Mar 5, 2024
1 parent 2b4d75f commit c9abe8a
Showing 1 changed file with 5 additions and 20 deletions.
25 changes: 5 additions & 20 deletions pkg/pipeline/sink/uploader/gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ import (
"github.com/googleapis/gax-go/v2"
"google.golang.org/api/googleapi"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
"google.golang.org/api/transport"
htransport "google.golang.org/api/transport/http"

"github.com/livekit/egress/pkg/errors"
"github.com/livekit/egress/pkg/types"
Expand All @@ -53,39 +50,27 @@ func newGCPUploader(conf *livekit.GCPUpload) (uploader, error) {
if conf.Credentials != "" {
opts = append(opts, option.WithCredentialsJSON([]byte(conf.Credentials)))
}
opts = append([]option.ClientOption{
option.WithScopes(storage.ScopeFullControl, "https://www.googleapis.com/auth/cloud-platform"),
option.WithUserAgent("gcloud-golang-storage/1.36.0")}, opts...)
opts = append(opts, internaloption.WithDefaultEndpoint("https://storage.googleapis.com/storage/v1/"))
opts = append(opts, internaloption.WithDefaultMTLSEndpoint("https://storage.mtls.googleapis.com/storage/v1/"))
creds, err := transport.Creds(context.Background(), opts...)
if err == nil {
opts = append(opts, internaloption.WithCredentials(creds))
}

// force ipv4 to avoid "service not available in your location, forbidden" errors from Google
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
// override default transport DialContext
defaultTransport := http.DefaultTransport.(*http.Transport).Clone()
http.DefaultTransport.(*http.Transport).DialContext = func(ctx context.Context, network, addr string) (net.Conn, error) {
return (&net.Dialer{
Timeout: time.Second * 30,
KeepAlive: time.Second * 30,
FallbackDelay: time.Nanosecond,
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)
}
httpClient, _, err := htransport.NewClient(context.Background(), opts...)
// reset default transport
http.DefaultTransport = defaultTransport

opts = append(opts, option.WithHTTPClient(httpClient))

c, err := storage.NewClient(context.Background(), opts...)
// restore default transport
http.DefaultTransport = defaultTransport
if err != nil {
return nil, err
}
Expand Down

0 comments on commit c9abe8a

Please sign in to comment.