Skip to content

Commit

Permalink
storage: Add emulator host to reader and writer
Browse files Browse the repository at this point in the history
Change-Id: I41cd692ddb9ac95b916b106dfadccf7d213646d8
Reviewed-on: https://code-review.googlesource.com/c/gocloud/+/42970
Reviewed-by: kokoro <[email protected]>
Reviewed-by: Jean de Klerk <[email protected]>
  • Loading branch information
acmp1 authored and odeke-em committed Aug 7, 2019
1 parent 706e5e4 commit e4bd323
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
4 changes: 2 additions & 2 deletions storage/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,8 +95,8 @@ func (o *ObjectHandle) NewRangeReader(ctx context.Context, offset, length int64)
}
}
u := &url.URL{
Scheme: "https",
Host: "storage.googleapis.com",
Scheme: o.c.scheme,
Host: o.c.readHost,
Path: fmt.Sprintf("/%s/%s", o.bucket, o.object),
}
verb := "GET"
Expand Down
22 changes: 20 additions & 2 deletions storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"fmt"
"net/http"
"net/url"
"os"
"reflect"
"regexp"
"sort"
Expand Down Expand Up @@ -82,6 +83,12 @@ func setClientHeader(headers http.Header) {
type Client struct {
hc *http.Client
raw *raw.Service
// Scheme describes the scheme under the current host.
scheme string
// EnvHost is the host set on the STORAGE_EMULATOR_HOST variable.
envHost string
// ReadHost is the default host used on the reader.
readHost string
}

// NewClient creates a new Google Cloud Storage client.
Expand All @@ -103,9 +110,20 @@ func NewClient(ctx context.Context, opts ...option.ClientOption) (*Client, error
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,
hc: hc,
raw: rawService,
scheme: scheme,
envHost: host,
readHost: readHost,
}, nil
}

Expand Down
4 changes: 4 additions & 0 deletions storage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,14 @@ func (w *Writer) open() error {
if w.MD5 != nil {
rawObj.Md5Hash = base64.StdEncoding.EncodeToString(w.MD5)
}
if w.o.c.envHost != "" {
w.o.c.raw.BasePath = fmt.Sprintf("%s://%s", w.o.c.scheme, w.o.c.envHost)
}
call := w.o.c.raw.Objects.Insert(w.o.bucket, rawObj).
Media(pr, mediaOpts...).
Projection("full").
Context(w.ctx)

if w.ProgressFunc != nil {
call.ProgressUpdater(func(n, _ int64) { w.ProgressFunc(n) })
}
Expand Down

0 comments on commit e4bd323

Please sign in to comment.