This repository has been archived by the owner on Apr 28, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstorage.go
183 lines (148 loc) · 3.77 KB
/
storage.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
package arias
import (
"cloud.google.com/go/storage"
"compress/gzip"
"context"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"google.golang.org/api/option"
"io"
"net/http"
)
type UploadOptions struct {
Bucket string
Filename string
ContentType string
// ForceGZip
ForceGZip bool
}
type UploadOutput struct {
Bucket string `json:"bucket"`
Filename string `json:"filename"`
}
type Storage interface {
Upload(ctx context.Context, f io.ReadSeeker, options UploadOptions) (UploadOutput, error)
}
func NewStorageFromType(storageType string) (s Storage, err error) {
switch storageType {
case "google":
s, err = NewGoogleCloudStorage()
case "s3":
s, err = NewS3Storage()
default:
err = fmt.Errorf("unknown storage: %s", storageType)
}
return
}
func determineContentType(f io.ReadSeeker, options UploadOptions) (contentType string, err error) {
contentType = options.ContentType
if contentType == "" {
buffer := make([]byte, 512)
_, err = f.Read(buffer)
if err != nil {
return
}
_, err = f.Seek(0, 0)
if err != nil {
return
}
contentType = http.DetectContentType(buffer)
}
return
}
type googleCloudStorage struct {
ctx context.Context
client *storage.Client
}
func NewGoogleCloudStorage(opts ...option.ClientOption) (s Storage, err error) {
ctx := context.Background()
client, err := storage.NewClient(ctx, opts...)
if err != nil {
return
}
s = &googleCloudStorage{
ctx: ctx,
client: client,
}
return
}
func (s *googleCloudStorage) Upload(ctx context.Context, f io.ReadSeeker, options UploadOptions) (UploadOutput, error) {
var out UploadOutput
bkt := s.client.Bucket(options.Bucket)
obj := bkt.Object(options.Filename)
objWriter := obj.NewWriter(ctx)
attrs := objWriter.ObjectAttrs
contentType, err := determineContentType(f, options)
if err != nil {
return out, nil
}
attrs.ContentType = contentType
attrs.ContentEncoding = "gzip"
// Google supports gzip "natively". It automatically decodes the data if need be
w, _ := gzip.NewWriterLevel(objWriter, gzip.BestCompression)
_, errWrite := io.Copy(w, f)
_ = w.Close()
_ = objWriter.Close()
if errWrite != nil {
return out, errWrite
}
objAttrs, err := obj.Attrs(ctx)
if err == nil {
out.Bucket = objAttrs.Bucket
out.Filename = objAttrs.Name
}
return out, nil
}
type s3Storage struct {
session *session.Session
uploader *s3manager.Uploader
}
func NewS3Storage(opts ...*aws.Config) (s Storage, err error) {
opt := aws.NewConfig().WithCredentials(credentials.NewEnvCredentials())
opts = append([]*aws.Config{opt}, opts...)
sess, err := session.NewSession(opts...)
if err != nil {
return
}
sess.Config.WithCredentialsChainVerboseErrors(true)
uploader := s3manager.NewUploader(sess)
s = &s3Storage{
session: sess,
uploader: uploader,
}
return
}
func (s *s3Storage) Upload(ctx context.Context, f io.ReadSeeker, options UploadOptions) (out UploadOutput, err error) {
contentType, err := determineContentType(f, options)
if err != nil {
return
}
var reader io.ReadSeeker
var contentEncoding *string
if options.ForceGZip {
contentEncoding = aws.String("gzip")
reader, writer := io.Pipe()
defer func() { _ = reader.Close() }()
go func() {
w, _ := gzip.NewWriterLevel(writer, gzip.BestCompression)
_, _ = io.Copy(w, f)
_ = w.Close()
_ = writer.Close()
}()
} else {
reader = f
}
input := s3manager.UploadInput{
Bucket: &options.Bucket,
Key: &options.Filename,
ContentType: &contentType,
ContentEncoding: contentEncoding,
Body: reader,
}
_, err = s.uploader.UploadWithContext(ctx, &input)
out = UploadOutput{options.Bucket, options.Filename}
return
}