Skip to content

Instantly share code, notes, and snippets.

@2minchul
Created December 5, 2022 09:08
Show Gist options
  • Select an option

  • Save 2minchul/23ae3e0edc4c1e0012b86d6f966a1a29 to your computer and use it in GitHub Desktop.

Select an option

Save 2minchul/23ae3e0edc4c1e0012b86d6f966a1a29 to your computer and use it in GitHub Desktop.

Revisions

  1. 2minchul created this gist Dec 5, 2022.
    43 changes: 43 additions & 0 deletions gcp_poxy.go
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,43 @@
    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")
    }