-
Notifications
You must be signed in to change notification settings - Fork 0
/
bucket.go
185 lines (146 loc) · 4.46 KB
/
bucket.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
package gcp
import (
"context"
"fmt"
"io"
"log"
"mime/multipart"
"os"
"cloud.google.com/go/storage"
"google.golang.org/api/option"
)
type ProviderGCP struct {
handler *storage.BucketHandle
}
func NewGCPBucket(ctx context.Context) (*ProviderGCP, error) {
// Option 1: Read credentials as pure json file
// Option 2: Encode the JSON file as env var
// cat /home/me/gcp-creds.json | base64
// export GCP_CREDS_JSON_BASE64="paste_base64_output_here"
// d, _ := base64.StdEncoding.DecodeString(os.GetEnv(GCP_CREDS_JSON_BASE64))
// Option 3: Use cryptography
// Another alternative is to use a cryptographic library to encrypt the file and
// pass the key as the environment variable to decrypt it during setup
cli, err := storage.NewClient(ctx, option.WithCredentialsFile(getCredentials()))
if err != nil {
return nil, err
}
return &ProviderGCP{handler: cli.Bucket(GetBucketName())}, nil
}
// func (b *ProviderGCP) setReadOnlyAccessPublic(ctx context.Context, objectName string) {
// acl := b.handler.Object(objectName).ACL()
// err := acl.Set(ctx, storage.AllUsers, storage.RoleReader)
// if err != nil {
// log.Println(err)
// }
// }
// func (b *ProviderGCP) getMidias(ctx context.Context, prefix string) ([]*dto.MidiaDTO, error) {
// q := &storage.Query{
// Prefix: prefix,
// Delimiter: "/",
// }
// it := b.handler.Objects(ctx, q)
// midias := []*dto.MidiaDTO{}
// for {
// attrs, err := it.Next()
// if err == iterator.Done {
// break
// }
// if err != nil {
// return nil, err
// }
// b.setReadOnlyAccessPublic(ctx, attrs.Name)
// midias = append(midias, buildMidia(attrs))
// }
// return midias, nil
// }
// func buildMidia(attrs *storage.ObjectAttrs) *dto.MidiaDTO {
// m := &dto.MidiaDTO{
// Name: attrs.Name,
// Type: attrs.ContentType,
// Link: "#",
// Size: attrs.Size,
// }
// link, err := makePublicLink(m.Name)
// if err != nil {
// log.Println(err)
// return m
// }
// m.Link = link
// return m
// }
// func makePublicLink(objectName string) (string, error) {
// key, err := ioutil.ReadFile(getCredentials())
// if err != nil {
// return "", err
// }
// cfg, err := google.JWTConfigFromJSON(key)
// if err != nil {
// return "", err
// }
// urlOptions := &storage.SignedURLOptions{
// GoogleAccessID: cfg.Email,
// PrivateKey: cfg.PrivateKey,
// Expires: time.Now().Add(time.Minute * 10),
// Method: http.MethodGet,
// }
// return storage.SignedURL(getBucketName(), objectName, urlOptions)
// }
// func (b *ProviderGCP) GetImages(ctx context.Context) ([]*dto.MidiaDTO, error) {
// images, err := b.getMidias(ctx, images_prefix)
// if err != nil {
// return nil, err
// }
// return filterMidias(images, "image"), nil
// }
// func (b *ProviderGCP) GetVideos(ctx context.Context) ([]*dto.MidiaDTO, error) {
// videos, err := b.getMidias(ctx, videos_prefix)
// if err != nil {
// return nil, err
// }
// return filterMidias(videos, "video"), nil
// }
// func (b *ProviderGCP) GetUploads(ctx context.Context) ([]*dto.MidiaDTO, error) {
// uploads, err := b.getMidias(ctx, uploads_prefix)
// if err != nil {
// return nil, err
// }
// filtered := filterMidias(uploads, "image")
// filtered = append(filtered, filterMidias(uploads, "video")...)
// return filtered, nil
// }
// func filterMidias(midias []*dto.MidiaDTO, contentType string) []*dto.MidiaDTO {
// filtered := []*dto.MidiaDTO{}
// for _, m := range midias {
// if strings.Contains(m.Type, contentType) {
// filtered = append(filtered, m)
// }
// }
// return filtered
// }
func (b *ProviderGCP) Upload(ctx context.Context, fh *multipart.FileHeader, uniqueName string, extension string) (string, error) {
log.Println("uploading the file in bucket GCP")
file, err := fh.Open()
if err != nil {
return "", err
}
defer file.Close()
writer := b.handler.Object(fmt.Sprintf("uploads/%s", uniqueName)).NewWriter(ctx)
writer.ContentType = fh.Header.Get("Content-Type")
writer.CacheControl = "public, max-age=86400"
// writer.Metadata = map[string]string{"api": "golang-upload-api"}
if _, err := io.Copy(writer, file); err != nil {
return "#", err
}
if err := writer.Close(); err != nil {
return "#", err
}
const publicLink = "https://storage.googleapis.com/%s/uploads/%s"
return fmt.Sprintf(publicLink, GetBucketName(), uniqueName), nil
}
func getCredentials() string {
return os.Getenv("GOOGLE_APPLICATION_CREDENTIALS")
}
func GetBucketName() string {
return os.Getenv("GOOGLE_BUCKET_NAME")
}