From 5e6ea8c5f2310dbd0b187bd97302b3e8272ce917 Mon Sep 17 00:00:00 2001 From: Brenna Epp Date: Fri, 20 Aug 2021 17:30:33 -0500 Subject: [PATCH 1/2] docs(storage): add comment in doc on how to use STORAGE_EMULATOR_HOST --- storage/doc.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/storage/doc.go b/storage/doc.go index 750e183496ac..8b3d71e910b9 100644 --- a/storage/doc.go +++ b/storage/doc.go @@ -48,6 +48,13 @@ an unauthenticated client with client, err := storage.NewClient(ctx, option.WithoutAuthentication()) +To use an emulator with this library, you can set the STORAGE_EMULATOR_HOST +environment variable to the address at which your emulator is running. This will +send requests to that address instead of to Cloud Storage. You can then create a +client as normal (see above) or, if you want, manually specify the endpoint: + + client, err := storage.NewClient(ctx, option.WithEndpoint(emulatorHost + "/storage/v1/")) + Buckets A Google Cloud Storage bucket is a collection of objects. To work with a From 43f3b71dc2a9f9e55bb0a99dc415dee5e07277e5 Mon Sep 17 00:00:00 2001 From: Brenna Epp Date: Tue, 24 Aug 2021 11:21:24 -0500 Subject: [PATCH 2/2] change sample to recommended way --- storage/doc.go | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/storage/doc.go b/storage/doc.go index 8b3d71e910b9..418e16068a9c 100644 --- a/storage/doc.go +++ b/storage/doc.go @@ -50,10 +50,28 @@ an unauthenticated client with To use an emulator with this library, you can set the STORAGE_EMULATOR_HOST environment variable to the address at which your emulator is running. This will -send requests to that address instead of to Cloud Storage. You can then create a -client as normal (see above) or, if you want, manually specify the endpoint: +send requests to that address instead of to Cloud Storage. You can then create +and use a client as usual: - client, err := storage.NewClient(ctx, option.WithEndpoint(emulatorHost + "/storage/v1/")) + // Set STORAGE_EMULATOR_HOST environment variable. + err := os.Setenv("STORAGE_EMULATOR_HOST", "localhost:9000") + if err != nil { + // TODO: Handle error. + } + + // Create client as usual. + client, err := storage.NewClient(ctx) + if err != nil { + // TODO: Handle error. + } + + // This request is now directed to http://localhost:9000/storage/v1/b + // instead of https://storage.googleapis.com/storage/v1/b + if err := client.Bucket("my-bucket").Create(ctx, projectID, nil); err != nil { + // TODO: Handle error. + } + +Please note that there is no official emulator for Cloud Storage. Buckets