-
Notifications
You must be signed in to change notification settings - Fork 0
/
storageAPI.go
136 lines (116 loc) · 2.88 KB
/
storageAPI.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
// Sample storage-quickstart creates a Google Cloud Storage bucket.
package googleApi
import (
"fmt"
"io"
"io/ioutil"
"log"
"os"
// Imports the Google Cloud Storage client package.
"cloud.google.com/go/storage"
"golang.org/x/net/context"
"google.golang.org/api/iterator"
)
func CreateBucket() {
ctx := context.Background()
// Sets your Google C loud Platform project ID.
projectID := os.Getenv("GOOGLE_PROJECT_ID")
// Creates a client.
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// Sets the name for the new bucket.
bucketName := os.Getenv("cloudStorage")
log.Println(bucketName)
// Creates a Bucket instance.
bucket := client.Bucket(bucketName)
// Creates the new bucket.
if err := bucket.Create(ctx, projectID, &storage.BucketAttrs{
StorageClass: "Multi-Regional",
Location: "asia",
}); err != nil {
log.Fatalf("Failed to create bucket: %v", err)
}
fmt.Printf("Bucket %v created.\n", bucketName)
}
func DeleteBucket(bucketName string) error {
ctx := context.Background()
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatalf("Failed to create client: %v", err)
}
// [START delete_bucket]
if err := client.Bucket(bucketName).Delete(ctx); err != nil {
return err
}
// [END delete_bucket]
return nil
}
func ListAPI() ([]string, error) {
ctx := context.Background()
client, err := storage.NewClient(ctx)
check(err)
bucket := os.Getenv("cloudStorage")
it := client.Bucket(bucket).Objects(ctx, nil)
var objects []string
for {
attrs, err := it.Next()
if err == iterator.Done {
break
}
if err != nil {
return nil, err
}
// log.Println(attrs.Name)
objects = append(objects, attrs.Name)
}
// [END storage_list_files]
return objects, nil
}
func StorageRead(object string) ([]byte, error) {
ctx := context.Background()
//Client
client, err := storage.NewClient(ctx)
check(err)
bucket := os.Getenv("cloudStorage")
rc, err := client.Bucket(bucket).Object(object).NewReader(ctx)
check(err)
defer rc.Close()
// log.Println("rc Read")
data, err := ioutil.ReadAll(rc)
// log.Println("Finish rc Read")
check(err)
return data, nil
}
func StorageUpload(object1 string) error {
ctx := context.Background()
//Client
client, err := storage.NewClient(ctx)
if err != nil {
log.Fatal(err)
}
f, err := os.Open("./audioFile/" + object1)
if err != nil {
log.Fatal(err)
}
defer f.Close()
//bucket
bucketName := os.Getenv("cloudStorage")
// bucket := client.Bucket(bucketName)
wc := client.Bucket(bucketName).Object(object1).NewWriter(ctx)
if _, err := io.Copy(wc, f); err != nil {
log.Println(err)
log.Panic(err)
}
if err := wc.Close(); err != nil {
log.Println(err)
log.Panic(err)
}
// open link
acl := client.Bucket(bucketName).Object(object1).ACL()
if err := acl.Set(ctx, storage.AllUsers, storage.RoleReader); err != nil {
log.Fatal(err)
}
return nil
}