Created
December 5, 2022 09:08
-
-
Save 2minchul/23ae3e0edc4c1e0012b86d6f966a1a29 to your computer and use it in GitHub Desktop.
Use http proxy in golang gcs package
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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