forked from golang-migrate/migrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
152 lines (130 loc) · 2.94 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
144
145
146
147
148
149
150
151
152
package awss3
import (
"fmt"
"io"
"net/url"
"os"
"path"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
"github.com/golang-migrate/migrate/v4/source"
)
func init() {
source.Register("s3", &s3Driver{})
}
type s3Driver struct {
s3client s3iface.S3API
config *Config
migrations *source.Migrations
}
type Config struct {
Bucket string
Prefix string
}
func (s *s3Driver) Open(folder string) (source.Driver, error) {
config, err := parseURI(folder)
if err != nil {
return nil, err
}
sess, err := session.NewSession()
if err != nil {
return nil, err
}
return WithInstance(s3.New(sess), config)
}
func WithInstance(s3client s3iface.S3API, config *Config) (source.Driver, error) {
driver := &s3Driver{
config: config,
s3client: s3client,
migrations: source.NewMigrations(),
}
if err := driver.loadMigrations(); err != nil {
return nil, err
}
return driver, nil
}
func parseURI(uri string) (*Config, error) {
u, err := url.Parse(uri)
if err != nil {
return nil, err
}
prefix := strings.Trim(u.Path, "/")
if prefix != "" {
prefix += "/"
}
return &Config{
Bucket: u.Host,
Prefix: prefix,
}, nil
}
func (s *s3Driver) loadMigrations() error {
output, err := s.s3client.ListObjects(&s3.ListObjectsInput{
Bucket: aws.String(s.config.Bucket),
Prefix: aws.String(s.config.Prefix),
Delimiter: aws.String("/"),
})
if err != nil {
return err
}
for _, object := range output.Contents {
_, fileName := path.Split(aws.StringValue(object.Key))
m, err := source.DefaultParse(fileName)
if err != nil {
continue
}
if !s.migrations.Append(m) {
return fmt.Errorf("unable to parse file %v", aws.StringValue(object.Key))
}
}
return nil
}
func (s *s3Driver) Close() error {
return nil
}
func (s *s3Driver) First() (uint, error) {
v, ok := s.migrations.First()
if !ok {
return 0, os.ErrNotExist
}
return v, nil
}
func (s *s3Driver) Prev(version uint) (uint, error) {
v, ok := s.migrations.Prev(version)
if !ok {
return 0, os.ErrNotExist
}
return v, nil
}
func (s *s3Driver) Next(version uint) (uint, error) {
v, ok := s.migrations.Next(version)
if !ok {
return 0, os.ErrNotExist
}
return v, nil
}
func (s *s3Driver) ReadUp(version uint) (io.ReadCloser, string, error) {
if m, ok := s.migrations.Up(version); ok {
return s.open(m)
}
return nil, "", os.ErrNotExist
}
func (s *s3Driver) ReadDown(version uint) (io.ReadCloser, string, error) {
if m, ok := s.migrations.Down(version); ok {
return s.open(m)
}
return nil, "", os.ErrNotExist
}
func (s *s3Driver) open(m *source.Migration) (io.ReadCloser, string, error) {
key := path.Join(s.config.Prefix, m.Raw)
object, err := s.s3client.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.config.Bucket),
Key: aws.String(key),
})
if err != nil {
return nil, "", err
}
return object.Body, m.Identifier, nil
}