-
Notifications
You must be signed in to change notification settings - Fork 0
/
store.go
103 lines (92 loc) · 2.54 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
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
package main
import (
"context"
"fmt"
"io/ioutil"
"os"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/credentials/ec2rolecreds"
"github.com/aws/aws-sdk-go/aws/ec2metadata"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/pkg/errors"
"gocloud.dev/blob"
"gocloud.dev/blob/s3blob"
)
func save(key string, payload string) (err error) {
ctx := context.Background()
b, err := setupAWS(ctx, bucket)
if err != nil {
log.WithError(err).Error("failed to setup bucket")
return err
}
err = b.WriteAll(ctx, key, []byte(payload), nil)
if err != nil {
log.WithError(err).Error("failed to write to bucket")
return err
}
log.Infof("Wrote out to s3://%s/%s", bucket, key)
return nil
}
func load(key string) (payload string, err error) {
ctx := context.Background()
b, err := setupAWS(ctx, bucket)
if err != nil {
return payload, errors.Wrap(err, "bucket setup")
}
r, err := b.NewReader(ctx, key, nil)
if err != nil {
return payload, errors.Wrap(err, "no reader")
}
defer r.Close()
// https://godoc.org/gocloud.dev/blob#Bucket.ReadAll
payloadbytes, err := ioutil.ReadAll(r)
if err != nil {
return payload, errors.Wrap(err, "failed to read")
}
payload = string(payloadbytes)
log.Infof("Read from to s3://%s/%s = %q", bucket, key, payload)
return
}
func del(key string) (err error) {
if key == "" {
return fmt.Errorf("Empty key")
}
ctx := context.Background()
b, err := setupAWS(ctx, bucket)
if err != nil {
log.WithError(err).Error("failed to setup bucket")
return err
}
err = b.Delete(ctx, key)
if err != nil {
log.WithError(err).Error("failed to delete")
}
return
}
// Using the AWS cloud
func setupAWS(ctx context.Context, bucket string) (b *blob.Bucket, err error) {
sess := session.New()
profile := os.Getenv("AWS_PROFILE")
if profile == "" {
profile = "uneet-dev"
}
creds := credentials.NewChainCredentials(
[]credentials.Provider{
// If you want to set AWS_ACCESS_KEY_ID & AWS_SECRET_ACCESS_KEY envs
&credentials.EnvProvider{},
// For when I use cmd/
&credentials.SharedCredentialsProvider{Filename: "", Profile: profile},
// IIUC, this is how IAM role is assumed in the Lambda env
&ec2rolecreds.EC2RoleProvider{Client: ec2metadata.New(sess)},
})
cfg := &aws.Config{
Region: aws.String("ap-southeast-1"),
Credentials: creds,
CredentialsChainVerboseErrors: aws.Bool(true),
}
sess, err = session.NewSession(cfg)
b, err = s3blob.OpenBucket(ctx, sess, bucket, nil)
return
}