// Copyright 2018 The Go Cloud Development Kit Authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // https://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. package gcsblob_test import ( "context" "log" "gocloud.dev/blob" "gocloud.dev/blob/gcsblob" "gocloud.dev/gcp" ) func ExampleOpenBucket() { // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. // PRAGMA: On gocloud.dev, hide lines until the next blank line. ctx := context.Background() // Your GCP credentials. // See https://cloud.google.com/docs/authentication/production // for more info on alternatives. creds, err := gcp.DefaultCredentials(ctx) if err != nil { log.Fatal(err) } // Create an HTTP client. // This example uses the default HTTP transport and the credentials // created above. client, err := gcp.NewHTTPClient( gcp.DefaultTransport(), gcp.CredentialsTokenSource(creds)) if err != nil { log.Fatal(err) } // Create a *blob.Bucket. bucket, err := gcsblob.OpenBucket(ctx, client, "my-bucket", nil) if err != nil { log.Fatal(err) } defer bucket.Close() } func Example_openBucketFromURL() { // PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. // PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/blob/gcsblob" // PRAGMA: On gocloud.dev, hide lines until the next blank line. ctx := context.Background() // blob.OpenBucket creates a *blob.Bucket from a URL. // This URL will open the bucket "my-bucket" using default credentials. bucket, err := blob.OpenBucket(ctx, "gs://my-bucket") if err != nil { log.Fatal(err) } defer bucket.Close() }