forked from GoogleCloudPlatform/golang-samples
/
listbuckets.go
60 lines (48 loc) · 1.32 KB
/
listbuckets.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// Copyright 2015 Google Inc. All rights reserved.
// Use of this source code is governed by the Apache 2.0
// license that can be found in the LICENSE file.
// Command listbuckets lists the Google Cloud buckets for a given project ID.
package main
import (
"fmt"
"log"
"os"
"google.golang.org/api/storage/v1"
"golang.org/x/net/context"
"golang.org/x/oauth2/google"
)
// ListBuckets returns a slice of all the buckets for a given project.
func ListBuckets(projectID string) ([]*storage.Bucket, error) {
ctx := context.Background()
// Create the client that uses Application Default Credentials
// See https://developers.google.com/identity/protocols/application-default-credentials
client, err := google.DefaultClient(ctx, storage.DevstorageReadOnlyScope)
if err != nil {
return nil, err
}
// Create the Google Cloud Storage service
service, err := storage.New(client)
if err != nil {
return nil, err
}
buckets, err := service.Buckets.List(projectID).Do()
if err != nil {
return nil, err
}
return buckets.Items, nil
}
func main() {
if len(os.Args) < 2 {
fmt.Println("usage: listbuckets <projectID>")
os.Exit(1)
}
project := os.Args[1]
buckets, err := ListBuckets(project)
if err != nil {
log.Fatal(err)
}
// Print out the results
for _, bucket := range buckets {
fmt.Println(bucket.Name)
}
}