forked from GoogleCloudPlatform/golang-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
110 lines (96 loc) · 2.59 KB
/
main.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2016 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.
// Sample buckets creates a bucket, lists buckets and deletes a bucket
// using the Google Storage API. More documentation is available at
// https://cloud.google.com/storage/docs/json_api/v1/.
package main
import (
"fmt"
"log"
"os"
"time"
"golang.org/x/net/context"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
)
func main() {
ctx := context.Background()
projectID := os.Getenv("GOOGLE_CLOUD_PROJECT")
if projectID == "" {
fmt.Fprintf(os.Stderr, "GOOGLE_CLOUD_PROJECT environment variable must be set.\n")
os.Exit(1)
}
// [START setup]
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
// [END setup]
// Give the bucket a unique name.
name := fmt.Sprintf("golang-example-buckets-%d", time.Now().Unix())
if err := create(client, projectID, name); err != nil {
log.Fatal(err)
}
fmt.Printf("created bucket: %v\n", name)
// list buckets from the project
buckets, err := list(client, projectID)
if err != nil {
log.Fatal(err)
}
fmt.Printf("buckets: %+v\n", buckets)
// delete the bucket
if err := delete(client, name); err != nil {
log.Fatal(err)
}
fmt.Printf("deleted bucket: %v\n", name)
}
func create(client *storage.Client, projectID, bucketName string) error {
ctx := context.Background()
// [START create_bucket]
if err := client.Bucket(bucketName).Create(ctx, projectID, nil); err != nil {
return err
}
// [END create_bucket]
return nil
}
func createWithAttrs(client *storage.Client, projectID, bucketName string) error {
ctx := context.Background()
// [START create_bucket_with_storageclass_and_location]
bucket := client.Bucket(bucketName)
if err := bucket.Create(ctx, projectID, &storage.BucketAttrs{
StorageClass: "COLDLINE",
Location: "asia",
}); err != nil {
return err
}
// [END create_bucket_with_storageclass_and_location]
return nil
}
func list(client *storage.Client, projectID string) ([]string, error) {
ctx := context.Background()
// [START list_buckets]
var buckets []string
it := client.Buckets(ctx, projectID)
for {
battrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
buckets = append(buckets, battrs.Name)
}
// [END list_buckets]
return buckets, nil
}
func delete(client *storage.Client, bucketName string) error {
ctx := context.Background()
// [START delete_bucket]
if err := client.Bucket(bucketName).Delete(ctx); err != nil {
return err
}
// [END delete_bucket]
return nil
}