storage: Updating default client settings when using STORAGE_EMULATOR_HOST.
Fixes #1653
Change-Id: I605f25a54f8f27a32367dad1f1daeeb6fa6a7c5a
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/48010
Reviewed-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Jean de Klerk <deklerk@google.com>
diff --git a/storage/storage.go b/storage/storage.go
index 43f55ee..f40b7ca 100644
--- a/storage/storage.go
+++ b/storage/storage.go
@@ -94,11 +94,20 @@
// NewClient creates a new Google Cloud Storage client.
// The default scope is ScopeFullControl. To use a different scope, like ScopeReadOnly, use option.WithScopes.
func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error) {
- o := []option.ClientOption{
- option.WithScopes(ScopeFullControl),
- option.WithUserAgent(userAgent),
+ var host, readHost, scheme string
+
+ if host = os.Getenv("STORAGE_EMULATOR_HOST"); host == "" {
+ scheme = "https"
+ readHost = "storage.googleapis.com"
+
+ opts = append(opts, option.WithScopes(ScopeFullControl), option.WithUserAgent(userAgent))
+ } else {
+ scheme = "http"
+ readHost = host
+
+ opts = append(opts, option.WithoutAuthentication())
}
- opts = append(o, opts...)
+
hc, ep, err := htransport.NewClient(ctx, opts...)
if err != nil {
return nil, fmt.Errorf("dialing: %v", err)
@@ -110,14 +119,7 @@
if ep != "" {
rawService.BasePath = ep
}
- scheme := "https"
- var host, readHost string
- if host = os.Getenv("STORAGE_EMULATOR_HOST"); host != "" {
- scheme = "http"
- readHost = host
- } else {
- readHost = "storage.googleapis.com"
- }
+
return &Client{
hc: hc,
raw: rawService,