forked from myussufz/cloud-storage
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgcs_adapter.go
172 lines (140 loc) · 4.3 KB
/
gcs_adapter.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
package storage
import (
"context"
"errors"
"fmt"
"io"
"io/ioutil"
"mime/multipart"
"strings"
"time"
s "cloud.google.com/go/storage"
)
const (
googleGCSDomain = "https://storage.googleapis.com"
)
// GoogleClient :
type GoogleClient struct {
Type string `json:"type"`
ProjectID string `json:"project_id"`
PrivateKeyID string `json:"private_key_id"`
PrivateKey string `json:"private_key"`
ClientID string `json:"client_id"`
ClientEmail string `json:"client_email"`
AuthURI string `json:"auth_uri"`
TokenURI string `json:"token_uri"`
AuthProviderX509CertURL string `json:"auth_provider_x509_cert_url"`
ClientX509CertURL string `json:"client_x509_cert_url"`
}
// GCSAdapter : The adapter empty because they do not have any data to share for now
type GCSAdapter struct{}
// UploadFile : Upload file to the bucket
func (adapter *GCSAdapter) UploadFile(file *multipart.FileHeader, bucket, filename string) (string, error) {
var reader io.Reader
name := ""
fileExt := strings.ToLower(strings.Split(file.Header["Content-Type"][0], "/")[1])
if ext, isExist := extensionMapper[fileExt]; isExist {
name = fmt.Sprintf("%s.%s", filename, ext)
} else {
name = fmt.Sprintf("%s.%s", filename, fileExt)
}
src, err := file.Open()
if err != nil {
return "", err
}
defer src.Close()
reader = src
fileURL, err := adapter.UploadReader(bucket, name, reader, strings.ToLower(fileExt))
if err != nil {
return "", err
}
return fileURL, nil
}
// DeleteFileUsingURL : Delete file from the bucket using url
func (adapter *GCSAdapter) DeleteFileUsingURL(bucket, fileURL string) error {
ctx := context.Background()
storageClient, err := s.NewClient(ctx)
if err != nil {
return err
}
defer storageClient.Close()
fileName := strings.Replace(fileURL, fmt.Sprintf("%s/%s/", googleGCSDomain, bucket), "", -1)
return storageClient.Bucket(bucket).Object(fileName).Delete(ctx)
}
// TemporaryServingFile : TemporaryServingFile file serving
func (adapter *GCSAdapter) TemporaryServingFile(bucket, fileURL string, expiredDateTime time.Time, googleClient interface{}) (string, error) {
credential := googleClient.(GoogleClient)
method := "GET"
fileName := strings.Replace(fileURL, fmt.Sprintf("%s/%s/", googleGCSDomain, bucket), "", -1)
url, err := s.SignedURL(bucket, fileName, &s.SignedURLOptions{
GoogleAccessID: credential.ClientEmail,
PrivateKey: []byte(credential.PrivateKey),
Method: method,
Expires: expiredDateTime,
})
if err != nil {
return "", err
}
return url, nil
}
// UploadReader :
func (adapter *GCSAdapter) UploadReader(bucket, filename string, reader io.Reader, contentType string) (string, error) {
ctx := context.Background()
storageClient, err := s.NewClient(ctx)
if err != nil {
return "", err
}
sw := storageClient.Bucket(bucket).Object(filename).NewWriter(ctx)
if contentType == "" {
contentTypeAny(sw)
} else {
contentFunc, isExist := contentTypeMapper[contentType]
if !isExist {
return "", fmt.Errorf("Content type %s does not supported", contentType)
}
contentFunc(sw)
}
if _, err := io.Copy(sw, reader); err != nil {
msg := fmt.Sprintf("Could not write file: %v", err)
return "", errors.New(msg)
}
if err := sw.Close(); err != nil {
msg := fmt.Sprintf("Could not put file: %v", err)
return "", errors.New(msg)
}
return fmt.Sprintf("%s/%s/%s", googleGCSDomain, bucket, filename), nil
}
// ReadFile :
func (adapter *GCSAdapter) ReadFile(bucket, path string) ([]byte, error) {
ctx := context.Background()
client, err := s.NewClient(ctx)
if err != nil {
return nil, err
}
defer client.Close()
rc, err := client.Bucket(bucket).Object(path).NewReader(ctx)
if err != nil {
return nil, err
}
defer rc.Close()
slurp, err := ioutil.ReadAll(rc)
if err != nil {
return nil, err
}
return slurp, nil
}
// UploadBuffer :
func (adapter *GCSAdapter) UploadBuffer(bucket, filename string, contentType string) (*Buffer, error) {
buf := new(Buffer)
buf.adapter = GCS
ctx := context.Background()
storageClient, err := s.NewClient(ctx)
if err != nil {
return nil, err
}
sw := storageClient.Bucket(bucket).Object(filename).NewWriter(ctx)
buf.storageWriter = sw
buf.bucket = bucket
contentTypeMapper[contentType](sw)
return buf, nil
}