googleapis / google-cloud-go

Google Cloud Client Libraries for Go.

Home Page:https://cloud.google.com/go/docs/reference

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Storage: empty readHost when STORAGE_EMULATOR_HOST is set to host:port

0xc0d opened this issue · comments

Client

Storage

Environment

Darwin Kernel Version 20.5.0

Go Environment

$ go version

go version go1.16 darwin/amd64

$ go env

GO111MODULE="on"
GOARCH="amd64"
GOBIN=""
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOINSECURE=""
GOOS="darwin"
GOSUMDB="sum.golang.org"
GOTMPDIR=""
GOVCS=""
GOVERSION="go1.16"
GCCGO="gccgo"
AR="ar"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD="/dev/null"
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -arch x86_64 -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/_r/pyfmdgr15fz3_x7hhfk7gb3r0000gp/T/go-build2794454919=/tmp/go-build -gno-record-gcc-switches -fno-common"

Code

package main

func main() {
	os.Setenv("STORAGE_EMULATOR_HOST", "localhost:4444")
	client, err := storage.NewClient(ctx)
	if err != nil {
		log.Fatal(err)
	}

	r, err := client.Bucket("bucket").Object("object").NewReader(context.TODO())
	if err != nil {
		log.Fatal(err)
	}
}

Expected behavior

return a reader to the object.

Actual behavior

Fatal with error: http: no Host in request URL,
URL: http:///bucket/object

Additional context

I believe this happens after this call. since WithEndpoint and WithDefaultEndpoint have different behaviour inside this function. There might be a modification there.

Hi @0xc0d, to use an emulator with the Go Storage Client you must set STORAGE_EMULATOR_HOST and manually supply the endpoint to the client as such:

client, err := storage.NewClient(context.Background(), option.WithEndpoint(EMULATOR_HOST+"/storage/v1/"))

However, I have linked a PR that will allow your code to work - the emulator will be set as the default endpoint for you. I added a test case for this as well. Please reopen if you continue having this issue once that is merged.