-
Notifications
You must be signed in to change notification settings - Fork 0
/
google.go
177 lines (160 loc) · 4.83 KB
/
google.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
package cstorage
import (
"cloud.google.com/go/storage"
"context"
"fmt"
"github.com/GabrielHCataldo/go-errors/errors"
"github.com/GabrielHCataldo/go-helper/helper"
"github.com/GabrielHCataldo/go-logger/logger"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"io"
)
type googleStorageClient struct {
client *storage.Client
}
// NewGoogleStorage new instance of connection with Google storage, to close it just use CStorage.Disconnect() or CStorage.SimpleDisconnect()
func NewGoogleStorage(ctx context.Context, opts ...option.ClientOption) (i CStorage, err error) {
client, err := storage.NewClient(ctx, opts...)
if helper.IsNil(err) {
i = &googleStorageClient{
client: client,
}
}
return i, err
}
func (g googleStorageClient) CreateBucket(ctx context.Context, input CreateBucketInput) error {
return g.client.Bucket(input.Bucket).Create(ctx, input.ProjectId, &storage.BucketAttrs{Location: input.Location})
}
func (g googleStorageClient) PutObject(ctx context.Context, input PutObjectInput) error {
obj := g.client.Bucket(input.Bucket).Object(input.Key)
fw := obj.NewWriter(ctx)
fw.ContentType = input.MimeType.String()
bytesContent, err := helper.ConvertToBytes(input.Content)
if helper.IsNil(err) {
_, err = fw.Write(bytesContent)
}
_ = fw.Close()
return err
}
func (g googleStorageClient) PutObjects(ctx context.Context, inputs ...PutObjectInput) []PutObjectOutput {
var result []PutObjectOutput
for _, input := range inputs {
err := g.PutObject(ctx, input)
result = append(result, PutObjectOutput{
Bucket: input.Bucket,
Key: input.Key,
Err: err,
})
}
return result
}
func (g googleStorageClient) GetObjectByKey(ctx context.Context, bucket, key string) (*Object, error) {
obj := g.client.Bucket(bucket).Object(key)
attrs, err := obj.Attrs(ctx)
if helper.IsNotNil(err) {
return nil, err
}
var bs []byte
reader, _ := obj.NewReader(ctx)
bs, err = io.ReadAll(reader)
objResult := parseGoogleStorageObject(attrs)
objResult.Url = g.GetObjectUrl(bucket, key)
objResult.Content = bs
return &objResult, err
}
func (g googleStorageClient) GetObjectUrl(bucket, key string) string {
url := "https://storage.googleapis.com/%s/%s"
return fmt.Sprintf(url, bucket, key)
}
func (g googleStorageClient) ListObjects(ctx context.Context, bucket string, opts ...*OptsListObjects) ([]ObjectSummary,
error) {
opt := MergeOptsListObjectsByParams(opts)
bkt := g.client.Bucket(bucket)
objs := bkt.Objects(ctx, &storage.Query{
Delimiter: opt.Delimiter,
Prefix: opt.Prefix,
})
var result []ObjectSummary
var rErr error
for {
obj, err := objs.Next()
if errors.Is(err, iterator.Done) {
break
} else if helper.IsNotNil(err) {
rErr = err
break
} else {
objResult := parseGoogleStorageObjectSummary(obj)
objResult.Url = g.GetObjectUrl(bucket, obj.Name)
result = append(result, objResult)
}
}
return result, rErr
}
func (g googleStorageClient) DeleteObject(ctx context.Context, input DeleteObjectInput) error {
bkt := g.client.Bucket(input.Bucket)
return bkt.Object(input.Key).Delete(ctx)
}
func (g googleStorageClient) DeleteObjects(ctx context.Context, inputs ...DeleteObjectInput) []DeleteObjectsOutput {
var result []DeleteObjectsOutput
for _, input := range inputs {
err := g.DeleteObject(ctx, input)
result = append(result, DeleteObjectsOutput{
Bucket: input.Bucket,
Key: input.Key,
Err: err,
})
}
return result
}
func (g googleStorageClient) DeleteObjectsByPrefix(ctx context.Context, input DeletePrefixInput) error {
bkt := g.client.Bucket(input.Bucket)
objs := bkt.Objects(ctx, &storage.Query{Prefix: input.Prefix})
var rErr error
for {
obj, err := objs.Next()
if errors.Is(err, iterator.Done) {
break
} else if helper.IsNotNil(err) {
rErr = err
break
} else {
rErr = bkt.Object(obj.Name).Delete(ctx)
}
}
return rErr
}
func (g googleStorageClient) DeleteObjectsByPrefixes(ctx context.Context, inputs ...DeletePrefixInput) []DeletePrefixOutput {
var result []DeletePrefixOutput
for _, input := range inputs {
err := g.DeleteObjectsByPrefix(ctx, input)
result = append(result, DeletePrefixOutput{
Bucket: input.Bucket,
Prefix: input.Prefix,
Err: err,
})
}
return result
}
func (g googleStorageClient) DeleteBucket(ctx context.Context, bucket string) error {
return g.client.Bucket(bucket).Delete(ctx)
}
func (g googleStorageClient) DeleteBuckets(ctx context.Context, buckets ...string) []DeleteBucketsOutput {
var result []DeleteBucketsOutput
for _, bucket := range buckets {
err := g.DeleteBucket(ctx, bucket)
result = append(result, DeleteBucketsOutput{
Bucket: bucket,
Err: err,
})
}
return result
}
func (g googleStorageClient) Disconnect() error {
return g.client.Close()
}
func (g googleStorageClient) SimpleDisconnect() {
_ = g.client.Close()
logger.InfoSkipCaller(3, "Connection to google storage closed.")
}