README.md
1# Google Cloud Client Libraries for Go
2
3[![GoDoc](https://godoc.org/cloud.google.com/go?status.svg)](https://godoc.org/cloud.google.com/go)
4
5Go packages for [Google Cloud Platform](https://cloud.google.com) services.
6
7``` go
8import "cloud.google.com/go"
9```
10
11To install the packages on your system, *do not clone the repo*. Instead use
12
13```
14$ go get -u cloud.google.com/go/...
15```
16
17**NOTE:** Some of these packages are under development, and may occasionally
18make backwards-incompatible changes.
19
20**NOTE:** Github repo is a mirror of [https://code.googlesource.com/gocloud](https://code.googlesource.com/gocloud).
21
22 * [News](#news)
23 * [Supported APIs](#supported-apis)
24 * [Go Versions Supported](#go-versions-supported)
25 * [Authorization](#authorization)
26 * [Cloud Datastore](#cloud-datastore-)
27 * [Cloud Storage](#cloud-storage-)
28 * [Cloud Pub/Sub](#cloud-pub-sub-)
29 * [BigQuery](#cloud-bigquery-)
30 * [Stackdriver Logging](#stackdriver-logging-)
31 * [Cloud Spanner](#cloud-spanner-)
32
33
34## News
35
36_7 August 2018_
37
38As of November 1, the code in the repo will no longer support Go versions 1.8
39and earlier. No one other than AppEngine users should be on those old versions,
40and AppEngine
41[Standard](https://groups.google.com/forum/#!topic/google-appengine-go/e7oPNomd7ak)
42and
43[Flex](https://groups.google.com/forum/#!topic/google-appengine-go/wHsYtxvEbXI)
44will stop supporting new deployments with those versions on that date.
45
46
47Changes have been moved to [CHANGES](https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/CHANGES.md).
48
49
50## Supported APIs
51
52Google API | Status | Package
53------------------------------------------------|--------------|-----------------------------------------------------------
54[Asset][cloud-asset] | alpha | [`cloud.google.com/go/asset/v1beta`][cloud-asset-ref]
55[BigQuery][cloud-bigquery] | stable | [`cloud.google.com/go/bigquery`][cloud-bigquery-ref]
56[Bigtable][cloud-bigtable] | stable | [`cloud.google.com/go/bigtable`][cloud-bigtable-ref]
57[Cloudtasks][cloud-tasks] | stable | [`cloud.google.com/go/cloudtasks/apiv2`][cloud-tasks-ref]
58[Container][cloud-container] | stable | [`cloud.google.com/go/container/apiv1`][cloud-container-ref]
59[ContainerAnalysis][cloud-containeranalysis] | beta | [`cloud.google.com/go/containeranalysis/apiv1beta1`][cloud-containeranalysis-ref]
60[Dataproc][cloud-dataproc] | stable | [`cloud.google.com/go/dataproc/apiv1`][cloud-dataproc-ref]
61[Datastore][cloud-datastore] | stable | [`cloud.google.com/go/datastore`][cloud-datastore-ref]
62[Debugger][cloud-debugger] | alpha | [`cloud.google.com/go/debugger/apiv2`][cloud-debugger-ref]
63[Dialogflow][cloud-dialogflow] | alpha | [`cloud.google.com/go/dialogflow/apiv2`][cloud-dialogflow-ref]
64[Data Loss Prevention][cloud-dlp] | alpha | [`cloud.google.com/go/dlp/apiv2`][cloud-dlp-ref]
65[ErrorReporting][cloud-errors] | alpha | [`cloud.google.com/go/errorreporting`][cloud-errors-ref]
66[Firestore][cloud-firestore] | stable | [`cloud.google.com/go/firestore`][cloud-firestore-ref]
67[IAM][cloud-iam] | stable | [`cloud.google.com/go/iam`][cloud-iam-ref]
68[IoT][cloud-iot] | alpha | [`cloud.google.com/iot/apiv1`][cloud-iot-ref]
69[KMS][cloud-kms] | stable | [`cloud.google.com/go/kms`][cloud-kms-ref]
70[Natural Language][cloud-natural-language] | stable | [`cloud.google.com/go/language/apiv1`][cloud-natural-language-ref]
71[Logging][cloud-logging] | stable | [`cloud.google.com/go/logging`][cloud-logging-ref]
72[Monitoring][cloud-monitoring] | alpha | [`cloud.google.com/go/monitoring/apiv3`][cloud-monitoring-ref]
73[OS Login][cloud-oslogin] | alpha | [`cloud.google.com/go/oslogin/apiv1`][cloud-oslogin-ref]
74[Pub/Sub][cloud-pubsub] | stable | [`cloud.google.com/go/pubsub`][cloud-pubsub-ref]
75[Phishing Protection][cloud-phishingprotection] | alpha | [`cloud.google.com/go/phishingprotection/apiv1betad1`][cloud-phishingprotection-ref]
76[reCAPTCHA Enterprise][cloud-recaptcha] | alpha | [`cloud.google.com/go/recaptchaenterprise/apiv1betad1`][cloud-recaptcha-ref]
77[Memorystore][cloud-memorystore] | alpha | [`cloud.google.com/go/redis/apiv1`][cloud-memorystore-ref]
78[Scheduler][cloud-scheduler] | stable | [`cloud.google.com/go/scheduler/apiv1`][cloud-scheduler-ref]
79[Spanner][cloud-spanner] | stable | [`cloud.google.com/go/spanner`][cloud-spanner-ref]
80[Speech][cloud-speech] | stable | [`cloud.google.com/go/speech/apiv1`][cloud-speech-ref]
81[Storage][cloud-storage] | stable | [`cloud.google.com/go/storage`][cloud-storage-ref]
82[Talent][cloud-talent] | alpha | [`cloud.google.com/go/talent/apiv4beta1`][cloud-talent-ref]
83[Text To Speech][cloud-texttospeech] | alpha | [`cloud.google.com/go/texttospeech/apiv1`][cloud-texttospeech-ref]
84[Trace][cloud-trace] | alpha | [`cloud.google.com/go/trace/apiv2`][cloud-trace-ref]
85[Translate][cloud-translate] | stable | [`cloud.google.com/go/translate`][cloud-translate-ref]
86[Video Intelligence][cloud-video] | alpha | [`cloud.google.com/go/videointelligence/apiv1beta1`][cloud-video-ref]
87[Vision][cloud-vision] | stable | [`cloud.google.com/go/vision/apiv1`][cloud-vision-ref]
88
89> **Alpha status**: the API is still being actively developed. As a
90> result, it might change in backward-incompatible ways and is not recommended
91> for production use.
92>
93> **Beta status**: the API is largely complete, but still has outstanding
94> features and bugs to be addressed. There may be minor backwards-incompatible
95> changes where necessary.
96>
97> **Stable status**: the API is mature and ready for production use. We will
98> continue addressing bugs and feature requests.
99
100Documentation and examples are available at
101https://godoc.org/cloud.google.com/go
102
103Visit or join the
104[google-api-go-announce group](https://groups.google.com/forum/#!forum/google-api-go-announce)
105for updates on these packages.
106
107## Go Versions Supported
108
109We support the two most recent major versions of Go. If Google App Engine uses
110an older version, we support that as well.
111
112## Authorization
113
114By default, each API will use [Google Application Default Credentials][default-creds]
115for authorization credentials used in calling the API endpoints. This will allow your
116application to run in many environments without requiring explicit configuration.
117
118[snip]:# (auth)
119```go
120client, err := storage.NewClient(ctx)
121```
122
123To authorize using a
124[JSON key file](https://cloud.google.com/iam/docs/managing-service-account-keys),
125pass
126[`option.WithCredentialsFile`](https://godoc.org/google.golang.org/api/option#WithCredentialsFile)
127to the `NewClient` function of the desired package. For example:
128
129[snip]:# (auth-JSON)
130```go
131client, err := storage.NewClient(ctx, option.WithCredentialsFile("path/to/keyfile.json"))
132```
133
134You can exert more control over authorization by using the
135[`golang.org/x/oauth2`](https://godoc.org/golang.org/x/oauth2) package to
136create an `oauth2.TokenSource`. Then pass
137[`option.WithTokenSource`](https://godoc.org/google.golang.org/api/option#WithTokenSource)
138to the `NewClient` function:
139[snip]:# (auth-ts)
140```go
141tokenSource := ...
142client, err := storage.NewClient(ctx, option.WithTokenSource(tokenSource))
143```
144
145## Cloud Datastore [![GoDoc](https://godoc.org/cloud.google.com/go/datastore?status.svg)](https://godoc.org/cloud.google.com/go/datastore)
146
147- [About Cloud Datastore][cloud-datastore]
148- [Activating the API for your project][cloud-datastore-activation]
149- [API documentation][cloud-datastore-docs]
150- [Go client documentation](https://godoc.org/cloud.google.com/go/datastore)
151- [Complete sample program](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/datastore/tasks)
152
153### Example Usage
154
155First create a `datastore.Client` to use throughout your application:
156
157[snip]:# (datastore-1)
158```go
159client, err := datastore.NewClient(ctx, "my-project-id")
160if err != nil {
161 log.Fatal(err)
162}
163```
164
165Then use that client to interact with the API:
166
167[snip]:# (datastore-2)
168```go
169type Post struct {
170 Title string
171 Body string `datastore:",noindex"`
172 PublishedAt time.Time
173}
174keys := []*datastore.Key{
175 datastore.NameKey("Post", "post1", nil),
176 datastore.NameKey("Post", "post2", nil),
177}
178posts := []*Post{
179 {Title: "Post 1", Body: "...", PublishedAt: time.Now()},
180 {Title: "Post 2", Body: "...", PublishedAt: time.Now()},
181}
182if _, err := client.PutMulti(ctx, keys, posts); err != nil {
183 log.Fatal(err)
184}
185```
186
187## Cloud Storage [![GoDoc](https://godoc.org/cloud.google.com/go/storage?status.svg)](https://godoc.org/cloud.google.com/go/storage)
188
189- [About Cloud Storage][cloud-storage]
190- [API documentation][cloud-storage-docs]
191- [Go client documentation](https://godoc.org/cloud.google.com/go/storage)
192- [Complete sample programs](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/storage)
193
194### Example Usage
195
196First create a `storage.Client` to use throughout your application:
197
198[snip]:# (storage-1)
199```go
200client, err := storage.NewClient(ctx)
201if err != nil {
202 log.Fatal(err)
203}
204```
205
206[snip]:# (storage-2)
207```go
208// Read the object1 from bucket.
209rc, err := client.Bucket("bucket").Object("object1").NewReader(ctx)
210if err != nil {
211 log.Fatal(err)
212}
213defer rc.Close()
214body, err := ioutil.ReadAll(rc)
215if err != nil {
216 log.Fatal(err)
217}
218```
219
220## Cloud Pub/Sub [![GoDoc](https://godoc.org/cloud.google.com/go/pubsub?status.svg)](https://godoc.org/cloud.google.com/go/pubsub)
221
222- [About Cloud Pubsub][cloud-pubsub]
223- [API documentation][cloud-pubsub-docs]
224- [Go client documentation](https://godoc.org/cloud.google.com/go/pubsub)
225- [Complete sample programs](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/pubsub)
226
227### Example Usage
228
229First create a `pubsub.Client` to use throughout your application:
230
231[snip]:# (pubsub-1)
232```go
233client, err := pubsub.NewClient(ctx, "project-id")
234if err != nil {
235 log.Fatal(err)
236}
237```
238
239Then use the client to publish and subscribe:
240
241[snip]:# (pubsub-2)
242```go
243// Publish "hello world" on topic1.
244topic := client.Topic("topic1")
245res := topic.Publish(ctx, &pubsub.Message{
246 Data: []byte("hello world"),
247})
248// The publish happens asynchronously.
249// Later, you can get the result from res:
250...
251msgID, err := res.Get(ctx)
252if err != nil {
253 log.Fatal(err)
254}
255
256// Use a callback to receive messages via subscription1.
257sub := client.Subscription("subscription1")
258err = sub.Receive(ctx, func(ctx context.Context, m *pubsub.Message) {
259 fmt.Println(m.Data)
260 m.Ack() // Acknowledge that we've consumed the message.
261})
262if err != nil {
263 log.Println(err)
264}
265```
266
267## BigQuery [![GoDoc](https://godoc.org/cloud.google.com/go/bigquery?status.svg)](https://godoc.org/cloud.google.com/go/bigquery)
268
269- [About BigQuery][cloud-bigquery]
270- [API documentation][cloud-bigquery-docs]
271- [Go client documentation][cloud-bigquery-ref]
272- [Complete sample programs](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/bigquery)
273
274### Example Usage
275
276First create a `bigquery.Client` to use throughout your application:
277[snip]:# (bq-1)
278```go
279c, err := bigquery.NewClient(ctx, "my-project-ID")
280if err != nil {
281 // TODO: Handle error.
282}
283```
284
285Then use that client to interact with the API:
286[snip]:# (bq-2)
287```go
288// Construct a query.
289q := c.Query(`
290 SELECT year, SUM(number)
291 FROM [bigquery-public-data:usa_names.usa_1910_2013]
292 WHERE name = "William"
293 GROUP BY year
294 ORDER BY year
295`)
296// Execute the query.
297it, err := q.Read(ctx)
298if err != nil {
299 // TODO: Handle error.
300}
301// Iterate through the results.
302for {
303 var values []bigquery.Value
304 err := it.Next(&values)
305 if err == iterator.Done {
306 break
307 }
308 if err != nil {
309 // TODO: Handle error.
310 }
311 fmt.Println(values)
312}
313```
314
315
316## Stackdriver Logging [![GoDoc](https://godoc.org/cloud.google.com/go/logging?status.svg)](https://godoc.org/cloud.google.com/go/logging)
317
318- [About Stackdriver Logging][cloud-logging]
319- [API documentation][cloud-logging-docs]
320- [Go client documentation][cloud-logging-ref]
321- [Complete sample programs](https://github.com/GoogleCloudPlatform/golang-samples/tree/master/logging)
322
323### Example Usage
324
325First create a `logging.Client` to use throughout your application:
326[snip]:# (logging-1)
327```go
328ctx := context.Background()
329client, err := logging.NewClient(ctx, "my-project")
330if err != nil {
331 // TODO: Handle error.
332}
333```
334
335Usually, you'll want to add log entries to a buffer to be periodically flushed
336(automatically and asynchronously) to the Stackdriver Logging service.
337[snip]:# (logging-2)
338```go
339logger := client.Logger("my-log")
340logger.Log(logging.Entry{Payload: "something happened!"})
341```
342
343Close your client before your program exits, to flush any buffered log entries.
344[snip]:# (logging-3)
345```go
346err = client.Close()
347if err != nil {
348 // TODO: Handle error.
349}
350```
351
352## Cloud Spanner [![GoDoc](https://godoc.org/cloud.google.com/go/spanner?status.svg)](https://godoc.org/cloud.google.com/go/spanner)
353
354- [About Cloud Spanner][cloud-spanner]
355- [API documentation][cloud-spanner-docs]
356- [Go client documentation](https://godoc.org/cloud.google.com/go/spanner)
357
358### Example Usage
359
360First create a `spanner.Client` to use throughout your application:
361
362[snip]:# (spanner-1)
363```go
364client, err := spanner.NewClient(ctx, "projects/P/instances/I/databases/D")
365if err != nil {
366 log.Fatal(err)
367}
368```
369
370[snip]:# (spanner-2)
371```go
372// Simple Reads And Writes
373_, err = client.Apply(ctx, []*spanner.Mutation{
374 spanner.Insert("Users",
375 []string{"name", "email"},
376 []interface{}{"alice", "a@example.com"})})
377if err != nil {
378 log.Fatal(err)
379}
380row, err := client.Single().ReadRow(ctx, "Users",
381 spanner.Key{"alice"}, []string{"email"})
382if err != nil {
383 log.Fatal(err)
384}
385```
386
387
388## Contributing
389
390Contributions are welcome. Please, see the
391[CONTRIBUTING](https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/CONTRIBUTING.md)
392document for details. We're using Gerrit for our code reviews. Please don't open pull
393requests against this repo, new pull requests will be automatically closed.
394
395Please note that this project is released with a Contributor Code of Conduct.
396By participating in this project you agree to abide by its terms.
397See [Contributor Code of Conduct](https://github.com/GoogleCloudPlatform/google-cloud-go/blob/master/CONTRIBUTING.md#contributor-code-of-conduct)
398for more information.
399
400[cloud-datastore]: https://cloud.google.com/datastore/
401[cloud-datastore-ref]: https://godoc.org/cloud.google.com/go/datastore
402[cloud-datastore-docs]: https://cloud.google.com/datastore/docs
403[cloud-datastore-activation]: https://cloud.google.com/datastore/docs/activate
404
405[cloud-firestore]: https://cloud.google.com/firestore/
406[cloud-firestore-ref]: https://godoc.org/cloud.google.com/go/firestore
407[cloud-firestore-docs]: https://cloud.google.com/firestore/docs
408[cloud-firestore-activation]: https://cloud.google.com/firestore/docs/activate
409
410[cloud-pubsub]: https://cloud.google.com/pubsub/
411[cloud-pubsub-ref]: https://godoc.org/cloud.google.com/go/pubsub
412[cloud-pubsub-docs]: https://cloud.google.com/pubsub/docs
413
414[cloud-storage]: https://cloud.google.com/storage/
415[cloud-storage-ref]: https://godoc.org/cloud.google.com/go/storage
416[cloud-storage-docs]: https://cloud.google.com/storage/docs
417[cloud-storage-create-bucket]: https://cloud.google.com/storage/docs/cloud-console#_creatingbuckets
418
419[cloud-bigtable]: https://cloud.google.com/bigtable/
420[cloud-bigtable-ref]: https://godoc.org/cloud.google.com/go/bigtable
421
422[cloud-bigquery]: https://cloud.google.com/bigquery/
423[cloud-bigquery-docs]: https://cloud.google.com/bigquery/docs
424[cloud-bigquery-ref]: https://godoc.org/cloud.google.com/go/bigquery
425
426[cloud-logging]: https://cloud.google.com/logging/
427[cloud-logging-docs]: https://cloud.google.com/logging/docs
428[cloud-logging-ref]: https://godoc.org/cloud.google.com/go/logging
429
430[cloud-monitoring]: https://cloud.google.com/monitoring/
431[cloud-monitoring-ref]: https://godoc.org/cloud.google.com/go/monitoring/apiv3
432
433[cloud-vision]: https://cloud.google.com/vision
434[cloud-vision-ref]: https://godoc.org/cloud.google.com/go/vision/apiv1
435
436[cloud-language]: https://cloud.google.com/natural-language
437[cloud-language-ref]: https://godoc.org/cloud.google.com/go/language/apiv1
438
439[cloud-oslogin]: https://cloud.google.com/compute/docs/oslogin/rest
440[cloud-oslogin-ref]: https://cloud.google.com/go/oslogin/apiv1
441
442[cloud-speech]: https://cloud.google.com/speech
443[cloud-speech-ref]: https://godoc.org/cloud.google.com/go/speech/apiv1
444
445[cloud-spanner]: https://cloud.google.com/spanner/
446[cloud-spanner-ref]: https://godoc.org/cloud.google.com/go/spanner
447[cloud-spanner-docs]: https://cloud.google.com/spanner/docs
448
449[cloud-translate]: https://cloud.google.com/translate
450[cloud-translate-ref]: https://godoc.org/cloud.google.com/go/translate
451
452[cloud-video]: https://cloud.google.com/video-intelligence/
453[cloud-video-ref]: https://godoc.org/cloud.google.com/go/videointelligence/apiv1beta1
454
455[cloud-errors]: https://cloud.google.com/error-reporting/
456[cloud-errors-ref]: https://godoc.org/cloud.google.com/go/errorreporting
457
458[cloud-container]: https://cloud.google.com/containers/
459[cloud-container-ref]: https://godoc.org/cloud.google.com/go/container/apiv1
460
461[cloud-debugger]: https://cloud.google.com/debugger/
462[cloud-debugger-ref]: https://godoc.org/cloud.google.com/go/debugger/apiv2
463
464[cloud-dlp]: https://cloud.google.com/dlp/
465[cloud-dlp-ref]: https://godoc.org/cloud.google.com/go/dlp/apiv2beta1
466
467[default-creds]: https://developers.google.com/identity/protocols/application-default-credentials
468
469[cloud-dataproc]: https://cloud.google.com/dataproc/
470[cloud-dataproc-docs]: https://cloud.google.com/dataproc/docs
471[cloud-dataproc-ref]: https://godoc.org/cloud.google.com/go/dataproc/apiv1
472
473[cloud-iam]: https://cloud.google.com/iam/
474[cloud-iam-docs]: https://cloud.google.com/iam/docs
475[cloud-iam-ref]: https://godoc.org/cloud.google.com/go/iam
476
477[cloud-kms]: https://cloud.google.com/kms/
478[cloud-kms-docs]: https://cloud.google.com/kms/docs
479[cloud-kms-ref]: https://godoc.org/cloud.google.com/go/kms/apiv1
480
481[cloud-natural-language]: https://cloud.google.com/natural-language/
482[cloud-natural-language-docs]: https://cloud.google.com/natural-language/docs
483[cloud-natural-language-ref]: https://godoc.org/cloud.google.com/go/language/apiv1
484
485[cloud-memorystore]: https://cloud.google.com/memorystore/
486[cloud-memorystore-docs]: https://cloud.google.com/memorystore/docs
487[cloud-memorystore-ref]: https://godoc.org/cloud.google.com/go/redis/apiv1
488
489[cloud-texttospeech]: https://cloud.google.com/texttospeech/
490[cloud-texttospeech-docs]: https://cloud.google.com/texttospeech/docs
491[cloud-texttospeech-ref]: https://godoc.org/cloud.google.com/go/texttospeech/apiv1
492
493[cloud-trace]: https://cloud.google.com/trace/
494[cloud-trace-docs]: https://cloud.google.com/trace/docs
495[cloud-trace-ref]: https://godoc.org/cloud.google.com/go/trace/apiv2
496
497[cloud-dialogflow]: https://cloud.google.com/dialogflow-enterprise/
498[cloud-dialogflow-docs]: https://cloud.google.com/dialogflow-enterprise/docs/
499[cloud-dialogflow-ref]: https://godoc.org/cloud.google.com/go/dialogflow/apiv2
500
501[cloud-containeranalysis]: https://cloud.google.com/container-registry/docs/container-analysis
502[cloud-containeranalysis-docs]: https://cloud.google.com/container-analysis/api/reference/rest/
503[cloud-containeranalysis-ref]: https://godoc.org/cloud.google.com/go/devtools/containeranalysis/apiv1beta1
504
505[cloud-asset]: https://cloud.google.com/security-command-center/docs/how-to-asset-inventory
506[cloud-asset-docs]: https://cloud.google.com/security-command-center/docs/how-to-asset-inventory
507[cloud-asset-ref]: https://godoc.org/cloud.google.com/go/asset/apiv1
508
509[cloud-tasks]: https://cloud.google.com/tasks/
510[cloud-tasks-ref]: https://godoc.org/cloud.google.com/go/cloudtasks/apiv2
511
512[cloud-scheduler]: https://cloud.google.com/scheduler
513[cloud-scheduler-ref]: https://godoc.org/cloud.google.com/go/scheduler/apiv1
514
515[cloud-iot]: https://cloud.google.com/iot-core/
516[cloud-iot-ref]: https://godoc.org/cloud.google.com/go/iot/apiv1
517
518[cloud-phishingprotection]: https://cloud.google.com/phishing-protection/
519[cloud-phishingprotection-ref]: https://cloud.google.com/go/phishingprotection/apiv1beta1
520
521[cloud-recaptcha]: https://cloud.google.com/recaptcha-enterprise/
522[cloud-recaptcha-ref]: https://cloud.google.com/go/recaptchaenterprise/apiv1beta1
523
524[cloud-talent]: https://cloud.google.com/solutions/talent-solution/
525[cloud-talent-ref]: https://godoc.org/cloud.google.com/go/talent/apiv4beta1
526