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
|
package controllers
import (
"context"
b64 "encoding/base64"
"fmt"
"io"
"log"
"net/http"
"os"
"github.com/gin-gonic/gin"
"golang.org/x/oauth2/google"
"golang.org/x/oauth2/jwt"
"google.golang.org/api/drive/v3"
)
func UploadDemo(c *gin.Context) {
// Check if user exists
/*user, exists := c.Get("user")
if !exists {
c.JSON(http.StatusUnauthorized, gin.H{
"code": http.StatusUnauthorized,
"output": gin.H{
"error": "User not logged in. Could be invalid token.",
},
})
return
} else {
user := user.(models.User)
c.JSON(http.StatusOK, gin.H{
"code": http.StatusOK,
"output": gin.H{
"avatar": user.AvatarLink,
"country": user.CountryCode,
"types": user.TypeToString(),
"username": user.Username,
},
"profile": true,
})
return
}*/
f, err := os.Open("test.txt")
if err != nil {
panic(fmt.Sprintf("cannot open file: %v", err))
}
defer f.Close()
client := serviceAccount()
srv, err := drive.New(client)
if err != nil {
log.Fatalf("Unable to retrieve drive Client %v", err)
}
file, err := createFile(srv, f.Name(), "application/octet-stream", f, os.Getenv("GOOGLE_FOLDER_ID"))
if err != nil {
panic(fmt.Sprintf("Could not create file: %v\n", err))
}
fmt.Printf("File '%s' successfully uploaded", file.Name)
fmt.Printf("\nFile Id: '%s' ", file.Id)
}
// Use Service account
func serviceAccount() *http.Client {
privateKey, _ := b64.StdEncoding.DecodeString(os.Getenv("GOOGLE_PRIVATE_KEY_BASE64"))
config := &jwt.Config{
Email: os.Getenv("GOOGLE_CLIENT_EMAIL"),
PrivateKey: []byte(privateKey),
Scopes: []string{
drive.DriveScope,
},
TokenURL: google.JWTTokenURL,
}
client := config.Client(context.Background())
return client
}
func createFile(service *drive.Service, name string, mimeType string, content io.Reader, parentId string) (*drive.File, error) {
f := &drive.File{
MimeType: mimeType,
Name: name,
Parents: []string{parentId},
}
file, err := service.Files.Create(f).Media(content).Do()
if err != nil {
log.Println("Could not create file: " + err.Error())
return nil, err
}
return file, nil
}
|