Last active
March 12, 2022 20:30
-
-
Save salrashid123/8a0ab7c548ae117c4b00c2d846ec0b33 to your computer and use it in GitHub Desktop.
logging GCS RequestResponse header and body in golang
This file contains hidden or 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 ( | |
"bytes" | |
"fmt" | |
"io/ioutil" | |
"log" | |
"mime/multipart" | |
"net/http" | |
storage "cloud.google.com/go/storage" | |
"golang.org/x/net/context" | |
"golang.org/x/oauth2/google" | |
"google.golang.org/api/option" | |
) | |
const ( | |
bucketName = "your-bucket" | |
) | |
var ( | |
body *bytes.Buffer | |
writer *multipart.Writer | |
) | |
type wrapped struct { | |
base http.RoundTripper | |
} | |
func (w wrapped) RoundTrip(r *http.Request) (*http.Response, error) { | |
log.Println("=======================================") | |
log.Println("[GCS_REQUEST]") | |
log.Printf("%v %v", r.Method, r.URL.String()) | |
for h, values := range r.Header { | |
for _, v := range values { | |
log.Printf("%v: %v\n", h, v) | |
} | |
} | |
body, err := ioutil.ReadAll(r.Body) | |
if err != nil { | |
log.Printf("Error reading body: %v", err) | |
return nil, err | |
} | |
fmt.Printf("%s", string(body)) | |
r.Body = ioutil.NopCloser(bytes.NewBuffer(body)) | |
resp, err := w.base.RoundTrip(r) | |
log.Printf("[GCS_RESPONSE]\n") | |
if err != nil { | |
log.Printf("GCS_ERROR: %v", err) | |
return resp, err | |
} | |
for h, values := range resp.Header { | |
for _, v := range values { | |
log.Printf("%v: %v\n", h, v) | |
} | |
} | |
log.Printf("Response Body: \n") | |
body, err = ioutil.ReadAll(resp.Body) | |
if err != nil { | |
log.Printf("Error reading body: %v", err) | |
return resp, err | |
} | |
fmt.Printf("%s", string(body)) | |
resp.Body = ioutil.NopCloser(bytes.NewBuffer(body)) | |
return resp, err | |
} | |
func main() { | |
ctx := context.Background() | |
body = &bytes.Buffer{} | |
writer = multipart.NewWriter(body) | |
hc, err := google.DefaultClient(ctx) | |
if err != nil { | |
log.Fatalf("%v", err) | |
} | |
hc.Transport = wrapped{hc.Transport} | |
storageClient, err := storage.NewClient(ctx, option.WithHTTPClient(hc)) | |
if err != nil { | |
fmt.Printf("storage.NewClient: %v", err) | |
return | |
} | |
defer storageClient.Close() | |
bkt := storageClient.Bucket(bucketName) | |
//https://cloud.google.com/storage/docs/json_api/v1/objects/insert | |
names := []string{"foo.txt", "bar.txt"} | |
for _, b := range names { | |
obj := bkt.Object(b) | |
w := obj.NewWriter(ctx) | |
if _, err := fmt.Fprintf(w, "foo"); err != nil { | |
fmt.Printf("write: %v", err) | |
return | |
} | |
if err := w.Close(); err != nil { | |
fmt.Printf("storage. close: %v", err) | |
return | |
} | |
//bkt.Object(b).Delete(ctx) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment