| 1 |
// Copyright 2018 The Go Cloud Development Kit Authors |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
package gcsblob_test |
| 16 |
|
| 17 |
import ( |
| 18 |
"context" |
| 19 |
"log" |
| 20 |
|
| 21 |
"gocloud.dev/blob" |
| 22 |
"gocloud.dev/blob/gcsblob" |
| 23 |
"gocloud.dev/gcp" |
| 24 |
) |
| 25 |
|
| 26 |
func ExampleOpenBucket() { |
| 27 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 28 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 29 |
ctx := context.Background() |
| 30 |
|
| 31 |
// Your GCP credentials. |
| 32 |
// See https://cloud.google.com/docs/authentication/production |
| 33 |
// for more info on alternatives. |
| 34 |
creds, err := gcp.DefaultCredentials(ctx) |
| 35 |
if err != nil { |
| 36 |
log.Fatal(err) |
| 37 |
} |
| 38 |
|
| 39 |
// Create an HTTP client. |
| 40 |
// This example uses the default HTTP transport and the credentials |
| 41 |
// created above. |
| 42 |
client, err := gcp.NewHTTPClient( |
| 43 |
gcp.DefaultTransport(), |
| 44 |
gcp.CredentialsTokenSource(creds)) |
| 45 |
if err != nil { |
| 46 |
log.Fatal(err) |
| 47 |
} |
| 48 |
|
| 49 |
// Create a *blob.Bucket. |
| 50 |
bucket, err := gcsblob.OpenBucket(ctx, client, "my-bucket", nil) |
| 51 |
if err != nil { |
| 52 |
log.Fatal(err) |
| 53 |
} |
| 54 |
defer bucket.Close() |
| 55 |
} |
| 56 |
|
| 57 |
func Example_openBucketFromURL() { |
| 58 |
// PRAGMA: This example is used on gocloud.dev; PRAGMA comments adjust how it is shown and can be ignored. |
| 59 |
// PRAGMA: On gocloud.dev, add a blank import: _ "gocloud.dev/blob/gcsblob" |
| 60 |
// PRAGMA: On gocloud.dev, hide lines until the next blank line. |
| 61 |
ctx := context.Background() |
| 62 |
|
| 63 |
// blob.OpenBucket creates a *blob.Bucket from a URL. |
| 64 |
// This URL will open the bucket "my-bucket" using default credentials. |
| 65 |
bucket, err := blob.OpenBucket(ctx, "gs://my-bucket") |
| 66 |
if err != nil { |
| 67 |
log.Fatal(err) |
| 68 |
} |
| 69 |
defer bucket.Close() |
| 70 |
} |