-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
207 lines (193 loc) · 6.01 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package main
import (
"context"
"github.com/GabrielHCataldo/go-cloud-storage/cstorage"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"github.com/aws/aws-sdk-go-v2/config"
"google.golang.org/api/option"
"os"
"time"
)
type testStruct struct {
Name string `json:"name,omitempty"`
BirthDate time.Time `json:"birthDate,omitempty"`
Balance float64 `json:"balance"`
Emails []string `json:"emails,omitempty"`
}
const bucketNameDefault = "go-cloud-storage-example"
func initTestStruct() testStruct {
return testStruct{
Name: "Foo Bar",
BirthDate: time.Now(),
Balance: 203.12,
Emails: []string{"foobar@gmail.com", "foobar2@gmail.com"},
}
}
func main() {
createBucket()
deleteBucket()
putObject()
getObjectByKey()
listObjects()
deleteObject()
deletePrefix()
}
func newInstanceAwsS3Storage() (cstorage.CStorage, error) {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
cfg, err := config.LoadDefaultConfig(ctx)
if helper.IsNotNil(err) {
return nil, err
}
return cstorage.NewAwsS3Storage(cfg), nil
}
func newInstanceGoogleStorage() (cstorage.CStorage, error) {
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
return cstorage.NewGoogleStorage(ctx, option.WithCredentialsFile("firebase-admin-sdk.json"))
}
func createBucket() {
cs, err := newInstanceAwsS3Storage() // or newInstanceAwsS3Storage
if helper.IsNotNil(err) {
logger.Error("error create new instance cloud storage:", err)
return
}
defer cs.SimpleDisconnect()
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
err = cs.CreateBucket(ctx, cstorage.CreateBucketInput{
// name of the bucket to be created (required)
Bucket: bucketNameDefault,
// project id where the bucket will be created (required only google storage)
ProjectId: os.Getenv("GOOGLE_STORAGE_PROJECT_ID"),
// bucket, if empty using default region
Location: "",
})
if helper.IsNotNil(err) {
logger.Error("error create bucket:", err)
} else {
logger.Info("bucket", bucketNameDefault, "created successfully!")
}
}
func deleteBucket() {
cs, err := newInstanceAwsS3Storage() // or newInstanceAwsS3Storage
if helper.IsNotNil(err) {
logger.Error("error create new instance cloud storage:", err)
return
}
defer cs.SimpleDisconnect()
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
err = cs.DeleteBucket(ctx, bucketNameDefault)
if helper.IsNotNil(err) {
logger.Error("error delete bucket:", err)
} else {
logger.Info("bucket", bucketNameDefault, "deleted successfully!")
}
}
func putObject() {
cs, err := newInstanceGoogleStorage() // or newInstanceAwsS3Storage
if helper.IsNotNil(err) {
logger.Error("error create new instance cloud storage:", err)
return
}
defer cs.SimpleDisconnect()
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
keyObject := "examples/json-example"
err = cs.PutObject(ctx, cstorage.PutObjectInput{
// name of the bucket where the object will be created (required)
Bucket: "go-cloud-storage",
// key of the object that will be created (required)
Key: keyObject,
// type of content of the object that will be created (required)
MimeType: cstorage.MimeTypeJson,
// content of the object that will be created (required)
Content: initTestStruct(),
})
if helper.IsNotNil(err) {
logger.Error("error put object on bucket:", err)
} else {
logger.Info("object", keyObject, "putted successfully!")
}
}
func getObjectByKey() {
cs, err := newInstanceGoogleStorage() // or newInstanceAwsS3Storage
if helper.IsNotNil(err) {
logger.Error("error create new instance cloud storage:", err)
return
}
defer cs.SimpleDisconnect()
keyObject := "examples/json-example"
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
obj, err := cs.GetObjectByKey(ctx, "go-cloud-storage", keyObject)
if helper.IsNotNil(err) {
logger.Error("error get object by key on bucket:", err)
} else {
var dest testStruct
err = obj.ParseContent(&dest)
if helper.IsNotNil(err) {
logger.Error("error parse object content:", err)
}
logger.Info("object", keyObject, "obtained successfully! obj:", obj, "content parsed:", dest)
}
}
func listObjects() {
cs, err := newInstanceGoogleStorage() // or newInstanceAwsS3Storage
if helper.IsNotNil(err) {
logger.Error("error create new instance cloud storage:", err)
return
}
defer cs.SimpleDisconnect()
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
opt := cstorage.NewOptsListObjects().SetPrefix("").SetDelimiter("")
objs, err := cs.ListObjects(ctx, "go-cloud-storage", opt)
if helper.IsNotNil(err) {
logger.Error("error list objects on bucket:", err)
} else {
logger.Info("list objects obtained successfully! objs:", objs)
}
}
func deleteObject() {
cs, err := newInstanceGoogleStorage() // or newInstanceAwsS3Storage
if helper.IsNotNil(err) {
logger.Error("error create new instance cloud storage:", err)
return
}
defer cs.SimpleDisconnect()
keyObject := "examples/json-example"
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
err = cs.DeleteObject(ctx, cstorage.DeleteObjectInput{
Bucket: "go-cloud-storage",
Key: keyObject,
})
if helper.IsNotNil(err) {
logger.Error("error delete object by key on bucket:", err)
} else {
logger.Info("object", keyObject, "deleted successfully!")
}
}
func deletePrefix() {
cs, err := newInstanceGoogleStorage() // or newInstanceAwsS3Storage
if helper.IsNotNil(err) {
logger.Error("error create new instance cloud storage:", err)
return
}
defer cs.SimpleDisconnect()
prefix := "examples/"
ctx, cancel := context.WithTimeout(context.TODO(), 5*time.Second)
defer cancel()
err = cs.DeleteObjectsByPrefix(ctx, cstorage.DeletePrefixInput{
Bucket: "go-cloud-storage",
Prefix: prefix,
})
if helper.IsNotNil(err) {
logger.Error("error delete objects by prefix on bucket:", err)
} else {
logger.Info("prefix", prefix, "deleted successfully!")
}
}