-
Notifications
You must be signed in to change notification settings - Fork 1
/
store.go
78 lines (65 loc) · 1.83 KB
/
store.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
package main
import (
"fmt"
"io"
"os"
"time"
"github.com/AdRoll/goamz/aws"
"github.com/AdRoll/goamz/s3"
"github.com/rlmcpherson/s3gof3r"
)
type Storer interface {
DownloadUrl(appId string, backupId string) (string, error)
// return bytes written
Put(appId string, backupId string, r io.Reader) (int64, error)
Delete(appId string, backupId string) error
}
type s3store struct {
bucketName string
bucket *s3gof3r.Bucket
regionName string
}
func NewS3Store(bucketName string, regionName string) (Storer, error) {
keys, err := s3gof3r.EnvKeys()
if err != nil {
return nil, err
}
regionName = getRegion(regionName)
s3Domain := fmt.Sprintf("s3.%s.amazonaws.com", regionName)
s3 := s3gof3r.New(s3Domain, keys)
bucket := s3.Bucket(bucketName)
return &s3store{bucketName: bucketName, bucket: bucket, regionName: regionName}, nil
}
func (s *s3store) DownloadUrl(appId string, backupId string) (string, error) {
auth, err := aws.EnvAuth()
if err != nil {
return "", err
}
svc := s3.New(auth, aws.GetRegion(s.regionName))
b := svc.Bucket(s.bucketName)
return b.SignedURL(s.pathFor(appId, backupId), time.Now().Add(20*time.Minute)), nil
}
func (s *s3store) Put(appId string, backupId string, r io.Reader) (int64, error) {
s3Path := s.pathFor(appId, backupId)
s3Putter, err := s.bucket.PutWriter(s3Path, nil, nil)
if err != nil {
return -1, err
}
defer s3Putter.Close()
return io.Copy(s3Putter, r)
}
func (s *s3store) Delete(appId string, backupId string) error {
return s.bucket.Delete(s.pathFor(appId, backupId))
}
func (*s3store) pathFor(appId string, backupId string) string {
return fmt.Sprintf("pgbackups/%s/%s.backup", appId, backupId)
}
func getRegion(regionName string) string {
if regionName == "" {
regionName = os.Getenv("AWS_REGION")
if regionName == "" {
regionName = "us-east-1"
}
}
return regionName
}