Skip to content

Instantly share code, notes, and snippets.

@2minchul
Created December 5, 2022 09:08
Show Gist options
  • Save 2minchul/23ae3e0edc4c1e0012b86d6f966a1a29 to your computer and use it in GitHub Desktop.
Save 2minchul/23ae3e0edc4c1e0012b86d6f966a1a29 to your computer and use it in GitHub Desktop.
Use http proxy in golang gcs package
package main
import (
"context"
"crypto/tls"
"io"
"net/http"
"net/url"
"cloud.google.com/go/storage"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
"google.golang.org/api/option"
)
func main() {
u, _ := url.Parse("http://127.0.0.1:8888")
ctx := context.Background()
cred, err := google.FindDefaultCredentials(ctx)
if err != nil {
panic(err)
}
client := http.Client{
Transport: &oauth2.Transport{
Base: &http.Transport{
Proxy: http.ProxyURL(u),
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Source: cred.TokenSource,
},
}
c, err := storage.NewClient(ctx, option.WithHTTPClient(&client))
r, err := c.Bucket("bucket").Object("object").NewReader(ctx)
if err != nil {
panic(err)
}
defer r.Close()
b, err := io.ReadAll(r)
if err != nil {
panic(err)
}
println("read", len(b), "bytes")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment