-
Notifications
You must be signed in to change notification settings - Fork 13
/
amazon_oss.go
258 lines (206 loc) · 6.81 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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package oss
import (
"bytes"
"context"
"encoding/base64"
"errors"
"strconv"
"time"
"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/coretrix/hitrix/datalayer"
"github.com/coretrix/hitrix/pkg/entity"
"github.com/coretrix/hitrix/service/component/clock"
"github.com/coretrix/hitrix/service/component/config"
)
type AmazonOSS struct {
client *s3.S3
clockService clock.IClock
ctx context.Context
buckets bucketsConfig
namespaces namespacesConfig
}
func NewAmazonOSS(configService config.IConfig, clockService clock.IClock, namespaces Namespaces) (IProvider, 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
}
bucketsConfiguration, namespacesConfiguration := loadConfig(configService, namespaces)
return &AmazonOSS{
client: s3.New(newSession),
clockService: clockService,
ctx: context.Background(),
buckets: bucketsConfiguration,
namespaces: namespacesConfiguration,
}, nil
}
type CachedObjectURLTemplate struct {
Environment string
BucketName string
StorageKey string
CounterID string
}
func (ossStorage *AmazonOSS) GetBucketConfig(bucket Bucket) *BucketConfig {
return getBucketConfig(ossStorage.buckets[bucket])
}
func (ossStorage *AmazonOSS) GetClient() interface{} {
return ossStorage.client
}
func (ossStorage *AmazonOSS) GetObjectURL(namespace Namespace, object *entity.FileObject) (string, error) {
cdnURL, err := ossStorage.GetObjectCDNURL(namespace, object)
if err != nil {
return "", err
}
if cdnURL != "" {
return cdnURL, nil
}
return ossStorage.GetObjectOSSURL(namespace, object)
}
func (ossStorage *AmazonOSS) GetObjectOSSURL(_ Namespace, _ *entity.FileObject) (string, error) {
panic("not implemented")
}
func (ossStorage *AmazonOSS) GetObjectCDNURL(namespace Namespace, object *entity.FileObject) (string, error) {
if object == nil {
return "", errors.New("nil file object")
}
bucketConfig, err := ossStorage.namespaces.getBucketConfig(namespace)
if err != nil {
return "", err
}
return getObjectCDNURL(bucketConfig, object.StorageKey), nil
}
func (ossStorage *AmazonOSS) GetObjectSignedURL(namespace Namespace, object *entity.FileObject, expires time.Time) (string, error) {
if object == nil {
return "", errors.New("nil file object")
}
now := ossStorage.clockService.Now()
if now.After(expires) {
return "", errors.New("expire time is before now")
}
bucketConfig, err := ossStorage.namespaces.getBucketConfig(namespace)
if err != nil {
return "", err
}
req, _ := ossStorage.client.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(bucketConfig.Name),
Key: aws.String(object.StorageKey)},
)
return req.Presign(expires.Sub(now))
}
func (ossStorage *AmazonOSS) GetObjectBase64Content(_ Namespace, _ *entity.FileObject) (string, error) {
panic("not implemented")
}
func (ossStorage *AmazonOSS) UploadObjectFromByte(
ormService *datalayer.ORM,
namespace Namespace,
objectContent []byte,
extension string,
) (entity.FileObject, error) {
bucketConfig, err := ossStorage.namespaces.getBucketConfig(namespace)
if err != nil {
return entity.FileObject{}, err
}
storageCounter := getStorageCounter(ossStorage.ctx, ormService, bucketConfig)
objectKey := ossStorage.getObjectKey(namespace, storageCounter, extension)
putObjectInput := &s3.PutObjectInput{
Body: bytes.NewReader(objectContent),
Bucket: aws.String(bucketConfig.Name),
Key: aws.String(objectKey),
}
if bucketConfig.ACL != "" {
putObjectInput.ACL = aws.String(bucketConfig.ACL)
}
_, err = ossStorage.client.PutObject(putObjectInput)
if err != nil {
return entity.FileObject{}, err
}
return entity.FileObject{
ID: storageCounter,
StorageKey: objectKey,
}, nil
}
func (ossStorage *AmazonOSS) UploadObjectFromFile(ormService *datalayer.ORM, namespace Namespace, localFile string) (entity.FileObject, error) {
fileContent, ext, err := readContentFile(localFile)
if err != nil {
return entity.FileObject{}, err
}
return ossStorage.UploadObjectFromByte(ormService, namespace, fileContent, ext)
}
func (ossStorage *AmazonOSS) UploadObjectFromBase64(
ormService *datalayer.ORM,
namespace Namespace,
content string,
extension string,
) (entity.FileObject, error) {
byteData, err := base64.StdEncoding.DecodeString(content)
if err != nil {
return entity.FileObject{}, err
}
return ossStorage.UploadObjectFromByte(ormService, namespace, byteData, extension)
}
func (ossStorage *AmazonOSS) GetNamespaceBucketConfig(namespace Namespace) (*BucketConfig, error) {
bucketConfig, err := ossStorage.namespaces.getBucketConfig(namespace)
if err != nil {
return nil, err
}
return bucketConfig, nil
}
func (ossStorage *AmazonOSS) UploadImageFromFile(ormService *datalayer.ORM, namespace Namespace, localFile string) (entity.FileObject, error) {
return ossStorage.UploadObjectFromFile(ormService, namespace, localFile)
}
func (ossStorage *AmazonOSS) UploadImageFromBase64(
ormService *datalayer.ORM,
namespace Namespace,
image string,
extension string,
) (entity.FileObject, error) {
byteData, err := base64.StdEncoding.DecodeString(image)
if err != nil {
return entity.FileObject{}, err
}
return ossStorage.UploadObjectFromByte(ormService, namespace, byteData, extension)
}
func (ossStorage *AmazonOSS) DeleteObject(namespace Namespace, object *entity.FileObject) error {
bucketConfig, err := ossStorage.namespaces.getBucketConfig(namespace)
if err != nil {
return err
}
_, err = ossStorage.client.DeleteObjects(&s3.DeleteObjectsInput{
Bucket: aws.String(bucketConfig.Name),
Delete: &s3.Delete{Objects: []*s3.ObjectIdentifier{{
Key: aws.String(object.StorageKey),
}}},
})
return err
}
func (ossStorage *AmazonOSS) getObjectKey(namespace Namespace, storageCounter uint64, fileExtension string) string {
return namespace.String() + "/" + strconv.FormatUint(storageCounter, 10) + "." + fileExtension
}