-
Notifications
You must be signed in to change notification settings - Fork 1
/
googleAPI.go
302 lines (235 loc) · 7.13 KB
/
googleAPI.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
package googleAPI
import (
"bufio"
"cloud.google.com/go/storage"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/dcbCIn/CloudStorage/cloudLib"
"github.com/dcbCIn/CloudStorage/shared"
"github.com/dcbCIn/MidCloud/lib"
"google.golang.org/api/iterator"
"google.golang.org/api/option"
"io/ioutil"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"time"
)
type JsonGoogleCloud struct {
Sku []Sku `json:"skus"`
}
type Sku struct {
Name string `json:"name"`
PricingInfo []PricingInfo `json:"pricingInfo"`
}
type PricingInfo struct {
PriceExpression PricingExpression `json:"pricingExpression"`
}
type PricingExpression struct {
TieredRates []TieredRate `json:"tieredRates"`
}
type TieredRate struct {
UnitPrice UnitPrice `json:"unitPrice"`
}
type UnitPrice struct {
Units string `json:"units"`
Nanos int `json:"nanos"`
}
type Google struct {
}
func (Google) Price(size float64) (price float64, err error) {
//return 3.1234
//jsonFile, err := os.Open("data.json")
url := "https://cloudbilling.googleapis.com/v1/services/95FF-2EF5-5EA1/skus?key=" + shared.GOOGLE_KEY_CODE
response, erro := http.Get(url)
if erro != nil {
//Caso tenha tido erro, ele é apresentado na tela
lib.PrintlnError("Erro ao abrir json. Erro", erro)
}
//defer jsonFile.Close()
// lendo o json do response do http request
responseJson, erro := ioutil.ReadAll(response.Body)
jsonGoogle := JsonGoogleCloud{}
erro = json.Unmarshal(responseJson, &jsonGoogle)
if erro != nil {
lib.PrintlnError("Erro ao realizar unmarshal. Erro:", erro)
}
for _, sku := range jsonGoogle.Sku {
if sku.Name == "services/95FF-2EF5-5EA1/skus/8A46-D6C4-859E" {
floatvalue, _ := strconv.ParseFloat(sku.PricingInfo[0].PriceExpression.TieredRates[0].UnitPrice.Units+"."+
strconv.Itoa(sku.PricingInfo[0].PriceExpression.TieredRates[0].UnitPrice.Nanos), 64)
price := size * floatvalue
return price, nil
}
}
// if price not found
return price, errors.New("Price not found. ")
}
func (Google) Availability() (available bool, err error) {
// TODO implement Google Availability
return true, nil
}
func (Google) SendFile(base64File string, fileName string, remotePath string) (createdFile cloudLib.CloudFile, err error) {
dtStart := time.Now()
/*dec, err := base64.StdEncoding.DecodeString(base64File)
if err != nil {
panic(err)
}
file, err := os.Create(fileName)
if err != nil {
panic(err)
}
defer file.Close()
if _, err := file.Write(dec); err != nil {
panic(err)
}*/
/*if err := file.Sync(); err != nil {
panic(err)
}*/
ctx := context.Background()
//projectID := "gifted-vigil-245219"
// este arquivo autentica aplicação no serviço do storage da google cloud storage
absPath, _ := filepath.Abs("./cloudLib/google/googleAPI/My First Project-41269a52f4a2.json")
client, err1 := storage.NewClient(ctx, option.WithCredentialsFile(absPath)) //"./CloudStorage/cloudLib/google/googleAPI/My First Project-41269a52f4a2.json"))
if err1 != nil {
log.Fatalln(err1)
}
bucketName := "midd_cloud"
bkt := client.Bucket(bucketName)
// O bucket midd_cloud já foi criado manualmente. O comando abaixo gera um novo bucket, por isso está comentado.
//if err := bkt.Create(ctx, projectID, nil); err != nil {
// log.Fatalf("Failed to create bucket: %v", err)
//}
attrs, err3 := bkt.Attrs(ctx)
if err3 != nil {
log.Fatalln(err3)
}
lib.PrintlnInfo("bucket", attrs.Name, " created at ", attrs.Created, ", is located in ", attrs.Location, " with storage class ", attrs.StorageClass)
obj := bkt.Object(remotePath + fileName)
w := obj.NewWriter(ctx)
// objAttrs, _ := obj.Attrs(ctx)
// objAttrs.ContentType = "image/jpg"
//obj.Update(ctx, attr)
//objAttrs := storage.ObjectAttrsToUpdate{ContentType: "image/jpg"}
//obj.Attrs := objAttrs
//obj.Update(ctx, objAttrs)
/*_, err = io.Copy(w, file)
if err != nil {
fmt.Println(err)
return
}
err = w.Close()
if err != nil {
fmt.Println(err)
return
}*/
// buffer := make([]byte, 1024)//fileInfo.Size())
decFile, err := base64.StdEncoding.DecodeString(base64File)
if err != nil {
panic(err)
}
w.Write(decFile)
/*for {
n, err := file.Read(buffer)
if err != nil && err != io.EOF {
fmt.Println(err)
return createdFile, err
}
if n == 0 {
break
}
if _, err := w.Write(buffer[:n]); err != nil {
fmt.Println(err)
return createdFile, err
}
}*/
if err4 := w.Close(); err != nil {
log.Fatalln(err4)
}
shared.LogEvent(shared.LOG, "googleAPI", "SendFile", "decodeAndWrite", "finished", "none", dtStart, time.Since(dtStart))
//fileInfo, _ := decFile.Stat()
//if err != nil {
// fmt.Println(err)
// return
//}
createdFile.Id = fileName
createdFile.Cloud = "Google Cloud Platform"
createdFile.Path = remotePath + fileName
size := (float64)(len(decFile)) / 1024 / 1024 // Convert to mb
createdFile.Size = fmt.Sprintf("%f", size) //strconv.FormatInt( fileInfo.Size(), 10)
createdFile.Created = time.Now()
createdFile.LastChecked = time.Now()
return createdFile, nil
}
func (Google) GetFile(fileName string, path string) (base64File string, err error) {
ctx := context.Background()
// este arquivo autentica aplicação no serviço do storage da google cloud storage
absPath, _ := filepath.Abs("./cloudLib/google/googleAPI/My First Project-41269a52f4a2.json")
fmt.Println(absPath)
client, err1 := storage.NewClient(ctx, option.WithCredentialsFile(absPath)) //"./CloudStorage/cloudLib/google/googleAPI/My First Project-41269a52f4a2.json"))
if err1 != nil {
log.Fatalln(err1)
}
bucketName := "midd_cloud"
rc, err2 := client.Bucket(bucketName).Object(path + fileName).NewReader(ctx)
if err2 != nil {
log.Fatalln(err2)
}
fmt.Print(rc)
defer rc.Close()
data, err3 := ioutil.ReadAll(rc)
if err3 != nil {
log.Fatalln(err3)
}
file, err2 := os.Create("./temp/" + fileName)
if err2 != nil {
fmt.Println(err2)
return
}
defer file.Close()
file.Write(data)
//fmt.Print(file.Stat())
reader := bufio.NewReader(file)
content, _ := ioutil.ReadAll(reader)
// Encode as base64.
base64File = base64.StdEncoding.EncodeToString([]byte(content))
fmt.Print(base64File)
return base64File, err
}
func (Google) List(path string) (files []cloudLib.CloudFile, err error) {
ctx := context.Background()
// este arquivo autentica aplicação no serviço do storage da google cloud storage
absPath, _ := filepath.Abs("./cloudLib/google/googleAPI/My First Project-41269a52f4a2.json")
fmt.Println(absPath)
client, err1 := storage.NewClient(ctx, option.WithCredentialsFile(absPath)) //"./CloudStorage/cloudLib/google/googleAPI/My First Project-41269a52f4a2.json"))
if err1 != nil {
log.Fatalln(err1)
}
bucketName := "midd_cloud"
it := client.Bucket(bucketName).Objects(ctx, nil)
i := 0
filesT := [1000]cloudLib.CloudFile{}
for {
attrs, err2 := it.Next()
if err2 == iterator.Done {
break
}
if err2 != nil {
log.Fatalln(err2)
}
filesT[i].Id = filepath.Base(attrs.Name)
filesT[i].Cloud = "Google Cloud Platform"
filesT[i].Path = path + filepath.Base(attrs.Name)
filesT[i].Size = strconv.FormatInt(attrs.Size, 10)
filesT[i].Created = attrs.Created
filesT[i].LastChecked = attrs.Created
i++
}
fmt.Print(filesT[0].Id)
return files, nil
}