-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
78 lines (64 loc) · 1.6 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
package main
import (
"context"
"fmt"
"io"
"io/ioutil"
"os"
"cloud.google.com/go/storage"
"golang.org/x/oauth2/google"
"google.golang.org/api/compute/v1"
"google.golang.org/api/option"
)
type teste struct {
nome string
telefone string
}
func main() {
var localFile string
var destinyFile string
var err error
var storageClient *storage.Client
fmt.Print("Enter local filepath: ")
fmt.Scanf("%s\n", &localFile)
fmt.Print("Enter destiny filepath: ")
fmt.Scanf("%s\n", &destinyFile)
bucket := "test_bucket_law"
ctx := context.Background()
data, err := ioutil.ReadFile("key.json")
if err != nil {
fmt.Printf("Erro1 %v\n", err.Error())
return
}
conf, err := google.JWTConfigFromJSON(data, compute.DevstorageFullControlScope)
if err != nil {
fmt.Printf("Erro2 %v\n", err.Error())
return
}
client := conf.Client(ctx)
// storageClient, err = storage.NewClient(ctx, option.WithCredentialsFile("key.json"))
storageClient, err = storage.NewClient(ctx, option.WithHTTPClient(client))
if err != nil {
fmt.Printf("Erro1 %v\n", err.Error())
return
}
f, err := os.Open(localFile)
if err != nil {
fmt.Printf("Erro3 %v\n", err.Error())
return
}
defer f.Close()
obj := storageClient.Bucket(bucket).Object(destinyFile)
sw := obj.NewWriter(ctx)
if _, err := io.Copy(sw, f); err != nil {
fmt.Printf("Erro4 %v\n", err.Error())
return
}
if err := sw.Close(); err != nil {
fmt.Printf("Erro5 %v\n", err.Error())
return
}
obj.ACL().Set(ctx, storage.AllUsers, storage.RoleReader)
obj.ACL().Set(ctx, storage.AllAuthenticatedUsers, storage.RoleWriter)
fmt.Printf("Sucesso!\n")
}