-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
143 lines (123 loc) · 3.46 KB
/
s3.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
package s3
import (
"bytes"
"io"
"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/aws/aws-sdk-go/service/s3/s3manager"
"github.com/timemore/foundation/errors"
mediastore "github.com/timemore/foundation/media/store"
"gopkg.in/yaml.v3"
)
type Config struct {
Region string `env:"REGION,required" yaml:"region" json:"region"`
BucketName string `env:"BUCKET_NAME,required" yaml:"bucket_name" json:"bucket_name"`
AccessKeyID string `env:"ACCESS_KEY_ID" yaml:"access_key_id" json:"access_key_id"`
SecretAccessKey string `env:"SECRET_ACCESS_KEY" yaml:"secret_access_key" json:"secret_access_key"`
}
const ServiceName = "s3"
func init() {
mediastore.RegisterModule(
ServiceName,
mediastore.Module{
NewService: NewService,
ServiceConfigSkeleton: func() mediastore.ServiceConfig {
cfg := ConfigSkeleton()
return &cfg
},
})
}
func ConfigSkeleton() Config { return Config{} }
func NewService(config mediastore.ServiceConfig) (mediastore.Service, error) {
if config == nil {
return nil, errors.ArgMsg("config", "missing")
}
conf, ok := config.(*Config)
if !ok {
b, _ := yaml.Marshal(config)
var cfg Config
err := yaml.Unmarshal(b, &cfg)
if err != nil {
return nil, errors.ArgMsg("config", "type invalid")
}
conf = &cfg
}
if conf == nil || conf.Region == "" || conf.BucketName == "" {
return nil, errors.ArgMsg("config", "fields invalid")
}
var creds *credentials.Credentials
if conf.AccessKeyID != "" {
creds = credentials.NewStaticCredentials(
conf.AccessKeyID,
conf.SecretAccessKey,
"",
)
}
sess, err := session.NewSession(&aws.Config{
Region: aws.String(conf.Region),
Credentials: creds,
})
if err != nil {
return nil, errors.Wrap("AWS Session", err)
}
const uploadPartSize = 10 * 1024 * 1024 // 10MiB
return &Service{
bucketName: conf.BucketName,
uploader: s3manager.NewUploader(sess, func(u *s3manager.Uploader) {
u.PartSize = uploadPartSize
}),
downloader: s3manager.NewDownloader(sess),
svc: s3.New(sess),
}, nil
}
type Service struct {
bucketName string
uploader *s3manager.Uploader
svc *s3.S3
downloader *s3manager.Downloader
}
func (s *Service) PutObject(targetKey string, contentSource io.Reader) (uploadInfo *mediastore.UploadInfo, err error) {
result, err := s.uploader.Upload(&s3manager.UploadInput{
Body: contentSource,
Bucket: aws.String(s.bucketName),
Key: aws.String(targetKey),
})
if err != nil {
return nil, errors.Wrap("upload", err)
}
return &mediastore.UploadInfo{
Bucket: *aws.String(s.bucketName),
Key: *aws.String(targetKey),
UploadID: result.UploadID,
Location: result.Location,
}, nil
}
func (s *Service) GetPublicObject(sourceKey string) (targetURL string, err error) {
req, _ := s.svc.GetObjectRequest(&s3.GetObjectInput{
Bucket: aws.String(s.bucketName),
Key: aws.String(sourceKey),
})
targetURL, err = req.Presign(15 * time.Minute)
if err != nil {
return "", err
}
return targetURL, nil
}
func (s *Service) GetObject(sourceKey string) (stream *bytes.Buffer, err error) {
buff := &aws.WriteAtBuffer{}
_, err = s.downloader.Download(buff,
&s3.GetObjectInput{
Bucket: aws.String(s.bucketName),
Key: aws.String(sourceKey),
})
if err != nil {
return nil, err
}
buf := new(bytes.Buffer)
buf.Read(buff.Bytes())
return buf, nil
}
var _ mediastore.Service = &Service{}