-
Notifications
You must be signed in to change notification settings - Fork 14
/
amazon_oss.go
218 lines (170 loc) · 5.94 KB
/
amazon_oss.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package oss
import (
"bytes"
"context"
"encoding/base64"
"errors"
"strconv"
"time"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
"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"
"github.com/latolukasz/beeorm"
)
type AmazonOSS struct {
client *s3.S3
clockService clock.IClock
ctx context.Context
env string
buckets *Buckets
uploaderBucket string
}
func NewAmazonOSS(configService config.IConfig, clockService clock.IClock, bucketsMapping map[string]uint64, env string) (*AmazonOSS, error) {
disableSSL := false
if val, ok := configService.Bool("oss.amazon.disable_ssl"); ok && val {
disableSSL = true
}
endpoint, ok := configService.String("oss.amazon.endpoint")
if !ok {
return nil, errors.New("missing endpoint")
}
accessKeyID, ok := configService.String("oss.amazon.access_key_id")
if !ok {
return nil, errors.New("missing access_key_id")
}
secretAccessKey, ok := configService.String("oss.amazon.secret_access_key")
if !ok {
return nil, errors.New("missing secret_access_key")
}
region, ok := configService.String("oss.amazon.region")
if !ok {
return nil, errors.New("missing region")
}
newSession, err := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(accessKeyID, secretAccessKey, ""),
Endpoint: aws.String(endpoint),
S3ForcePathStyle: aws.Bool(true),
Region: aws.String(region),
DisableSSL: aws.Bool(disableSSL),
})
if err != nil {
return nil, err
}
bucket, _ := configService.String("oss.uploader.bucket")
return &AmazonOSS{
client: s3.New(newSession),
clockService: clockService,
ctx: context.Background(),
env: env,
buckets: loadBucketsConfig(configService, bucketsMapping),
uploaderBucket: bucket,
}, nil
}
type CachedObjectURLTemplate struct {
Environment string
BucketName string
StorageKey string
CounterID string
}
func (ossStorage *AmazonOSS) GetClient() interface{} {
return ossStorage.client
}
func (ossStorage *AmazonOSS) GetObjectURL(bucket string, object *Object) (string, error) {
cdnURL, err := ossStorage.GetObjectCDNURL(bucket, object)
if err != nil {
return "", err
}
if cdnURL != "" {
return cdnURL, nil
}
return ossStorage.GetObjectOSSURL(bucket, object)
}
func (ossStorage *AmazonOSS) GetObjectOSSURL(_ string, _ *Object) (string, error) {
panic("not implemented")
}
func (ossStorage *AmazonOSS) GetObjectCDNURL(bucket string, object *Object) (string, error) {
return getObjectCDNURL(
ossStorage.buckets,
bucket,
ossStorage.env,
object.StorageKey), nil
}
func (ossStorage *AmazonOSS) GetObjectSignedURL(bucket string, object *Object, expires time.Time) (string, error) {
now := ossStorage.clockService.Now()
if now.After(expires) {
return "", errors.New("expire time is before now")
}
req, _ := ossStorage.client.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(getBucketName(ossStorage.buckets, bucket, ossStorage.env)),
Key: aws.String(object.StorageKey)},
)
return req.Presign(expires.Sub(now))
}
func (ossStorage *AmazonOSS) GetObjectBase64Content(_ string, _ *Object) (string, error) {
panic("not implemented")
}
func (ossStorage *AmazonOSS) UploadObjectFromByte(ormService *beeorm.Engine, bucket string, objectContent []byte, extension string) (Object, error) {
storageCounter := getStorageCounter(ormService, ossStorage.buckets, bucket)
objectKey := ossStorage.getObjectKey(storageCounter, extension)
_, err := ossStorage.client.PutObject(&s3.PutObjectInput{
Body: bytes.NewReader(objectContent),
Bucket: aws.String(getBucketName(ossStorage.buckets, bucket, ossStorage.env)),
Key: aws.String(objectKey),
})
if err != nil {
return Object{}, err
}
return Object{
ID: storageCounter,
StorageKey: objectKey,
}, nil
}
func (ossStorage *AmazonOSS) UploadObjectFromFile(ormService *beeorm.Engine, bucket, localFile string) (Object, error) {
fileContent, ext, err := readContentFile(localFile)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, fileContent, ext)
}
func (ossStorage *AmazonOSS) UploadObjectFromBase64(ormService *beeorm.Engine, bucket, content, extension string) (Object, error) {
byteData, err := base64.StdEncoding.DecodeString(content)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, byteData, extension)
}
func (ossStorage *AmazonOSS) UploadImageFromFile(ormService *beeorm.Engine, bucket, localFile string) (Object, error) {
return ossStorage.UploadObjectFromFile(ormService, bucket, localFile)
}
func (ossStorage *AmazonOSS) UploadImageFromBase64(ormService *beeorm.Engine, bucket, image, extension string) (Object, error) {
byteData, err := base64.StdEncoding.DecodeString(image)
if err != nil {
return Object{}, err
}
return ossStorage.UploadObjectFromByte(ormService, bucket, byteData, extension)
}
func (ossStorage *AmazonOSS) DeleteObject(bucket string, object *Object) error {
_, err := ossStorage.client.DeleteObjects(&s3.DeleteObjectsInput{
Bucket: aws.String(getBucketName(ossStorage.buckets, bucket, ossStorage.env)),
Delete: &s3.Delete{Objects: []*s3.ObjectIdentifier{{
Key: aws.String(object.StorageKey),
}}},
})
return err
}
func (ossStorage *AmazonOSS) getObjectKey(storageCounter uint64, fileExtension string) string {
return strconv.FormatUint(storageCounter, 10) + fileExtension
}
func (ossStorage *AmazonOSS) CreateObjectFromKey(ormService *beeorm.Engine, bucket, key string) Object {
//TODO remove
return Object{
ID: getStorageCounter(ormService, ossStorage.buckets, bucket),
StorageKey: key,
}
}
func (ossStorage *AmazonOSS) GetUploaderBucketConfig() *BucketConfig {
return getBucketEnvConfig(ossStorage.buckets, ossStorage.uploaderBucket, ossStorage.env)
}