forked from simplesurance/baur
/
s3.go
100 lines (79 loc) · 1.97 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
package s3
import (
"fmt"
"net/url"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/simplesurance/baur/log"
)
// Client is a S3 uploader client
type Client struct {
sess *session.Session
uploader *s3manager.Uploader
}
// DefaultRetries is the number of retries for a S3 upload until an error is
// raised
const DefaultRetries = 3
// NewClient returns a new S3 Client, configuration is read from env variables
// or configuration files,
// see https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/configuring-sdk.html
func NewClient() (*Client, error) {
cfg := aws.Config{
Logger: &log.S3Logger{},
LogLevel: aws.LogLevel(aws.LogDebug),
MaxRetries: aws.Int(DefaultRetries),
S3ForcePathStyle: aws.Bool(true),
}
sess, err := session.NewSession(&cfg)
if err != nil {
return nil, err
}
return &Client{sess: sess,
uploader: s3manager.NewUploader(sess),
}, nil
}
func bucketFromURL(u *url.URL) string {
return u.Host
}
func fileFromURL(u *url.URL) string {
return u.Path
}
func verifyURL(u *url.URL) error {
if u.Scheme != "s3" {
return fmt.Errorf("unsupported URL scheme '%s'", u.Scheme)
}
if len(u.Host) == 0 {
return fmt.Errorf("bucket missing in url '%s'", u)
}
if len(u.Path) == 0 {
return fmt.Errorf("filename missing in url '%s'", u)
}
return nil
}
// Upload uploads a file to an s3 bucket, On success it returns the URL to the
// file.
func (c *Client) Upload(file string, dest string) (string, error) {
url, err := url.Parse(dest)
if err != nil {
return "", err
}
if err := verifyURL(url); err != nil {
return "", err
}
f, err := os.Open(file)
if err != nil {
return "", err
}
defer f.Close()
res, err := c.uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucketFromURL(url)),
Key: aws.String(fileFromURL(url)),
Body: f,
})
if err != nil {
return "", err
}
return res.Location, err
}