This repository has been archived by the owner on Oct 8, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
s3.go
84 lines (72 loc) · 1.96 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
package s3
import (
"bytes"
"os"
"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/s3iface"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/s3/s3manager/s3manageriface"
"github.com/pkg/errors"
)
type ClientAPI interface {
List() ([]string, error)
Put(key, contentType string, data []byte) error
Delete(key string) error
}
type Client struct {
api s3iface.S3API
uploader s3manageriface.UploaderAPI
bucket string
}
func New(bucket string) *Client {
awscfg := &aws.Config{Region: aws.String("us-east-1")}
if os.Getenv("STAGE") == "local" {
awscfg.Endpoint = aws.String("http://localhost:9000")
awscfg.Credentials = credentials.NewStaticCredentials("DUMMY_AWS_ACCESS_KEY_ID", "DUMMY_AWS_SECRET_ACCESS_KEY", "")
awscfg.S3ForcePathStyle = aws.Bool(true)
}
sess := session.Must(session.NewSession(awscfg))
return &Client{
api: s3.New(sess),
uploader: s3manager.NewUploader(sess),
bucket: bucket,
}
}
func (cl *Client) List() ([]string, error) {
resp, err := cl.api.ListObjectsV2(&s3.ListObjectsV2Input{
Bucket: aws.String(cl.bucket),
})
if err != nil {
return nil, errors.WithStack(err)
}
var keys []string
for _, c := range resp.Contents {
keys = append(keys, *c.Key)
}
return keys, nil
}
func (cl *Client) Put(key, contentType string, data []byte) error {
_, err := cl.uploader.Upload(&s3manager.UploadInput{
Bucket: aws.String(cl.bucket),
Key: aws.String(key),
ContentType: aws.String(contentType),
Body: bytes.NewReader(data),
})
if err != nil {
return errors.WithStack(err)
}
return nil
}
func (cl *Client) Delete(key string) error {
_, err := cl.api.DeleteObject(&s3.DeleteObjectInput{
Bucket: aws.String(cl.bucket),
Key: aws.String(key),
})
if err != nil {
return errors.WithStack(err)
}
return nil
}