/
GCPServices.go
211 lines (174 loc) · 4.89 KB
/
GCPServices.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
208
209
210
211
// Package sty_shared
/*
This is the STY-Holdings shared services
NOTES:
None
COPYRIGHT & WARRANTY:
Copyright (c) 2022 STY-Holdings, inc
All rights reserved.
This software is the confidential and proprietary information of STY-Holdings, Inc.
Use is subject to license terms.
Unauthorized copying of this file, via any medium is strictly prohibited.
Proprietary and confidential
Written by <Replace with FULL_NAME> / syacko
STY-Holdings, Inc.
support@sty-holdings.com
www.sty-holdings.com
01-2024
USA
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package sty_shared
import (
"context"
"io/ioutil"
"log"
"os"
"runtime"
"cloud.google.com/go/storage"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
)
var (
ctx = context.Background()
)
// CreateStorageClient - connect to Google Cloud Platform services
//
// Customer Messages: None
// Errors: Return an errors generated by GCP with Ending Execution appended
// Verifications: None
func CreateStorageClient(
credentialsFile string,
test bool,
) (
client *storage.Client,
errorInfo pi.ErrorInfo,
) {
var (
tFunction, _, _, _ = runtime.Caller(0)
tFunctionName = runtime.FuncForPC(tFunction).Name()
)
pi.PrintDebugTrail(tFunctionName)
if client, errorInfo.Error = storage.NewClient(ctx, option.WithCredentialsJSON(getGCPKey(credentialsFile, test))); errorInfo.Error != nil {
log.Println(errorInfo.Error.Error(), ctv.ENDING_EXECUTION)
}
return
}
// getBucket - return a pointer to a storage bucket
//
// Customer Messages: None
// Errors: ErrRequiredArgumentMissing
// Verifications: storageClientPtr is not nil
func getBucket(
storageClientPtr *storage.Client,
bucketName string,
) (
bucketPtr *storage.BucketHandle,
errorInfo pi.ErrorInfo,
) {
if storageClientPtr == nil || bucketName == ctv.EMPTY {
errorInfo.Error = pi.ErrRequiredArgumentMissing
} else {
// Create a bucket object for the specified bucket.
bucketPtr = storageClientPtr.Bucket(bucketName)
}
return
}
// getGCPKey - will read the JSON key file. If either fail, exit is called.
//
// Customer Messages: None
// Errors: ErrUnableReadFile
// Validations: File readable
func getGCPKey(
GCPCredentialsFQN string,
test bool,
) (GCPCredentials []byte) {
var (
errorInfo pi.ErrorInfo
tFunction, _, _, _ = runtime.Caller(0)
tFunctionName = runtime.FuncForPC(tFunction).Name()
)
pi.PrintDebugTrail(tFunctionName)
if GCPCredentials, errorInfo.Error = os.ReadFile(GCPCredentialsFQN); errorInfo.Error != nil {
errorInfo.Error = pi.ErrUnableReadFile
log.Println(errorInfo.Error.Error())
}
if errorInfo.Error != nil && test == false {
os.Exit(1)
}
return
}
// ListFilesInBucket - return all the object names in a bucket, folders and files
//
// Customer Messages: None
// Errors: ErrRequiredArgumentMissing
// Verifications: storageClientPtr is not nil
func ListObjectsInBucket(
storageClientPtr *storage.Client,
bucketName string,
) (
bucketList []string,
errorInfo pi.ErrorInfo,
) {
var (
tBucketPtr *storage.BucketHandle
tObjectAttributes *storage.ObjectAttrs
tObjectIterator *storage.ObjectIterator
)
if storageClientPtr == nil || bucketName == ctv.EMPTY {
errorInfo.Error = pi.ErrRequiredArgumentMissing
} else {
tBucketPtr, errorInfo = getBucket(storageClientPtr, bucketName)
// Create a list object for the bucket.
tObjectIterator = tBucketPtr.Objects(ctx, nil)
for {
tObjectAttributes, errorInfo.Error = tObjectIterator.Next()
if errorInfo.Error == iterator.Done {
errorInfo.Error = nil
break
}
if errorInfo.Error != nil {
break
}
bucketList = append(bucketList, tObjectAttributes.Name)
}
}
return
}
// ReadBucketObject - returns the contains of the bucket's named file.
//
// Customer Messages: None
// Errors: ErrRequiredArgumentMissing
// Verifications: bucketPtr is not nil
func ReadBucketObject(
storageClientPtr *storage.Client,
bucketName string,
fileName string,
) (
contents []byte,
errorInfo pi.ErrorInfo,
) {
var (
tBucketPtr *storage.BucketHandle
tReader *storage.Reader
)
if storageClientPtr == nil || bucketName == ctv.EMPTY || fileName == ctv.EMPTY {
errorInfo.Error = pi.ErrRequiredArgumentMissing
} else {
if tBucketPtr, errorInfo = getBucket(storageClientPtr, bucketName); errorInfo.Error == nil {
// Create an object for the specified file.
if tReader, errorInfo.Error = tBucketPtr.Object(fileName).NewReader(context.Background()); errorInfo.Error == nil {
defer func(tReader *storage.Reader) {
_ = tReader.Close()
}(tReader)
// Read the contents of the reader into a byte slice.
contents, errorInfo.Error = ioutil.ReadAll(tReader)
}
}
}
return
}