Skip to content

Instantly share code, notes, and snippets.

@Slach
Created October 1, 2021 12:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Slach/57c5bb8331bcbc32e998166557220ad4 to your computer and use it in GitHub Desktop.
Save Slach/57c5bb8331bcbc32e998166557220ad4 to your computer and use it in GitHub Desktop.
Google Cloud Storage (GCS) debug options
import (
"context"
"fmt"
"io"
"net/http"
"path"
"strings"
"time"
"cloud.google.com/go/storage"
"github.com/AlexAkulov/clickhouse-backup/config"
"github.com/apex/log"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"google.golang.org/api/option/internaloption"
googleHTTPTransport "google.golang.org/api/transport/http"
)
// GCS - presents methods for manipulate data on GCS
type GCS struct {
client *storage.Client
Config *config.GCSConfig
buffer []byte
}
type debugGCSTransport struct {
base http.RoundTripper
}
func (w debugGCSTransport) RoundTrip (r *http.Request) (*http.Response, error) {
log.Info("=======================================")
log.Info("[GCS_REQUEST]")
log.Infof("%v %v", r.Method, r.URL.String())
for h, values := range r.Header {
for _, v := range values {
log.Infof("%v: %v", h, v)
}
}
resp, err := w.base.RoundTrip(r)
log.Info("[GCS_RESPONSE]")
if err != nil {
log.Errorf("GCS_ERROR: %v", err)
return resp, err
}
for h, values := range resp.Header {
for _, v := range values {
log.Infof("%v: %v", h, v)
}
}
return resp, err
}
// Connect - connect to GCS
func (gcs *GCS) Connect() error {
var err error
clientOptions := make([]option.ClientOption, 0)
gcs.buffer = make([]byte, 4*1024*1024)
ctx := context.Background()
if gcs.Config.CredentialsJSON != "" {
clientOptions = append(clientOptions, option.WithCredentialsJSON([]byte(gcs.Config.CredentialsJSON)))
} else if gcs.Config.CredentialsFile != "" {
clientOptions = append(clientOptions, option.WithCredentialsFile(gcs.Config.CredentialsFile))
}
if gcs.Config.Debug {
clientOptions = append([]option.ClientOption{option.WithScopes(storage.ScopeFullControl)}, clientOptions...)
clientOptions = append(clientOptions, internaloption.WithDefaultEndpoint("https://storage.googleapis.com/storage/v1/"))
clientOptions = append(clientOptions, internaloption.WithDefaultMTLSEndpoint("https://storage.mtls.googleapis.com/storage/v1/"))
debugClient, _, err := googleHTTPTransport.NewClient(ctx, clientOptions...)
if err !=nil {
return fmt.Errorf("googleHTTPTransport.NewClient error: %v", err)
}
debugClient.Transport = debugGCSTransport{base: debugClient.Transport}
clientOptions = append(clientOptions, option.WithHTTPClient(debugClient))
}
gcs.client, err = storage.NewClient(ctx, clientOptions...)
return err
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment