-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
google_drive_utils.go
133 lines (112 loc) · 3.21 KB
/
google_drive_utils.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
package google_drive_utils
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"time"
"github.com/sikalabs/slu/utils/vault_google_drive_utils"
"golang.org/x/oauth2"
drive "google.golang.org/api/drive/v3"
"google.golang.org/api/option"
)
func Upload(clientId, clientSecret, accessToken, fileToUpload string) {
if clientId == "" && clientSecret == "" {
clientId, clientSecret = GetGoogleDriveUploadSecretsFromVaultOrEnvOrDie()
}
if accessToken == "" {
accessToken = vault_google_drive_utils.GetGoogleDriveUploadTokenSecretsFromVaultOrEnvOrDie()
}
conf := &oauth2.Config{
ClientID: clientId,
ClientSecret: clientSecret,
Scopes: []string{drive.DriveFileScope},
Endpoint: oauth2.Endpoint{
TokenURL: "https://oauth2.googleapis.com/token",
},
}
ctx := context.Background()
token := &oauth2.Token{
AccessToken: accessToken,
}
client := conf.Client(ctx, token)
srv, err := drive.NewService(ctx, option.WithHTTPClient(client))
if err != nil {
log.Fatalf("Unable to retrieve Drive client: %v", err)
}
f, err := os.Open(fileToUpload)
if err != nil {
log.Fatalf("Failed to open file: %s", err)
}
defer f.Close()
fileMetadata := &drive.File{
Name: filepath.Base(fileToUpload),
}
file, err := srv.Files.Create(fileMetadata).Media(f).Do()
if err != nil {
log.Fatalf("Failed to create file: %s", err)
}
fmt.Printf("File ID: %s\n", file.Id)
}
func GetToken(clientId, clientSecret string) string {
if clientId == "" && clientSecret == "" {
clientId, clientSecret = GetGoogleDriveUploadSecretsFromVaultOrEnvOrDie()
}
ctx := context.Background()
conf := &oauth2.Config{
ClientID: clientId,
ClientSecret: clientSecret,
Scopes: []string{"https://www.googleapis.com/auth/drive.file"},
Endpoint: oauth2.Endpoint{
TokenURL: "https://oauth2.googleapis.com/token",
DeviceAuthURL: "https://oauth2.googleapis.com/device/code",
},
}
code, err := conf.DeviceAuth(ctx)
if err != nil {
log.Fatalf("Failed to get device and user codes: %v", err)
}
fmt.Printf("Visit the URL: %s\n", code.VerificationURI)
fmt.Printf("And enter the code: %s\n", code.UserCode)
for {
token, err := conf.DeviceAccessToken(ctx, code)
if err == nil {
fmt.Printf("Got access token: %s\n", token.AccessToken)
fmt.Printf("Got refresh token: %s\n", token.RefreshToken)
return token.AccessToken
}
time.Sleep(1 * time.Second)
}
}
func GetGoogleDriveUploadSecretsFromVaultOrEnvOrDie() (
string, string,
) {
clientIdVault, clientSecretVault,
_ := vault_google_drive_utils.GetGoogleDriveUploadSecrets("secret/data/slu/google-drive-upload/client")
// Client ID
var clientId string
clientIdEnv := os.Getenv("SLU_GOOGLE_DRIVE_UPLOAD_CLIENT_ID")
if clientIdVault != "" {
clientId = clientIdVault
}
if clientIdEnv != "" {
clientId = clientIdEnv
}
if clientId == "" {
log.Fatalln("SLU_GOOGLE_DRIVE_UPLOAD_CLIENT_ID is empty")
}
// Client Secret
var clientSecret string
clientSecretEnv := os.Getenv("SLU_GOOGLE_DRIVE_UPLOAD_CLIENT_SECRET")
if clientIdVault != "" {
clientSecret = clientSecretVault
}
if clientIdEnv != "" {
clientSecret = clientSecretEnv
}
if clientSecret == "" {
log.Fatalln("SLU_GOOGLE_DRIVE_UPLOAD_CLIENT_SECRET is empty")
}
return clientId, clientSecret
}