-
Notifications
You must be signed in to change notification settings - Fork 1
/
storage.go
95 lines (83 loc) · 2.16 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
package nopaste
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"path"
"path/filepath"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
type Storage interface {
Save(string, []byte) error
Load(string) (io.ReadCloser, error)
}
type LocalStorage struct {
DataDir string
}
func NewLocalStorage(datadir string) *LocalStorage {
return &LocalStorage{
DataDir: datadir,
}
}
func (s *LocalStorage) Save(name string, data []byte) error {
f := filepath.Join(s.DataDir, name+".txt")
log.Println("[debug] save to", f)
return ioutil.WriteFile(f, data, 0644)
}
func (s *LocalStorage) Load(name string) (io.ReadCloser, error) {
f := filepath.Join(s.DataDir, name+".txt")
log.Println("[debug] load from", f)
return os.Open(f)
}
type S3Storage struct {
Bucket string
KeyPrefix string
svc *s3.S3
}
func NewS3Storage(c *S3Config) *S3Storage {
sess := session.Must(session.NewSession())
svc := s3.New(sess)
return &S3Storage{
Bucket: c.Bucket,
KeyPrefix: c.KeyPrefix,
svc: svc,
}
}
func (s *S3Storage) Load(name string) (io.ReadCloser, error) {
for _, name := range []string{s.objectName(name), name} {
result, err := s.svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(s.Bucket),
Key: aws.String(path.Join(s.KeyPrefix, name)),
})
log.Printf("[debug] load from s3://%s", path.Join(s.Bucket, s.KeyPrefix, name))
if err == nil {
log.Println("[debug] result", result.GoString())
return result.Body, nil
}
}
return nil, fmt.Errorf("Not found %s and %s", name, s.objectName(name))
}
func (s *S3Storage) Save(name string, b []byte) error {
name = s.objectName(name)
input := &s3.PutObjectInput{
Body: aws.ReadSeekCloser(bytes.NewReader(b)),
Bucket: aws.String(s.Bucket),
Key: aws.String(path.Join(s.KeyPrefix, name)),
ContentType: aws.String("text/plain"),
}
log.Printf("[debug] save to s3://%s", path.Join(s.Bucket, s.KeyPrefix, name))
_, err := s.svc.PutObject(input)
return err
}
func (s *S3Storage) objectName(name string) string {
if len(name) > 5 {
return path.Join(name[0:2], name[2:4], name[4:])
} else {
return name
}
}