-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
79 lines (64 loc) · 2.03 KB
/
main.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
package s3
import (
"context"
"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"
"gitlab.com/distributed_lab/logan/v3/errors"
)
var (
defaultRequestPayer = "requester"
Delimiter = "/"
)
type Config struct {
Bucket string `fig:"bucket,required"`
Region string `fig:"region,required"`
AccessKey string `fig:"access_key,required"`
SecretAccessKey string `fig:"secret_access_key,required"`
}
type Connector interface {
GetObject(ctx context.Context, key string) (*s3.GetObjectOutput, error)
ListObjects(ctx context.Context, limit int64, startAfter string) (*s3.ListObjectsV2Output, error)
}
type connector struct {
client *s3.S3
Config
}
func New(cfg Config) (Connector, error) {
awsCfg := aws.NewConfig().
WithCredentials(credentials.NewStaticCredentials(cfg.AccessKey, cfg.SecretAccessKey, "")).
WithRegion(cfg.Region)
sess, err := session.NewSession(awsCfg)
if err != nil {
return nil, err
}
res := &connector{client: s3.New(sess), Config: cfg}
return res, nil
}
func (c *connector) GetObject(ctx context.Context, key string) (*s3.GetObjectOutput, error) {
input := &s3.GetObjectInput{
Key: aws.String(key),
Bucket: aws.String(c.Config.Bucket),
RequestPayer: aws.String(defaultRequestPayer),
}
res, err := c.client.GetObjectWithContext(ctx, input)
if err != nil {
return nil, errors.Wrap(err, "failed to get object from s3")
}
return res, nil
}
func (c *connector) ListObjects(ctx context.Context, limit int64, startAfter string) (*s3.ListObjectsV2Output, error) {
input := &s3.ListObjectsV2Input{
Bucket: aws.String(c.Config.Bucket),
MaxKeys: aws.Int64(limit),
StartAfter: aws.String(startAfter),
Delimiter: aws.String(Delimiter),
RequestPayer: aws.String(defaultRequestPayer),
}
res, err := c.client.ListObjectsV2WithContext(ctx, input)
if err != nil {
return nil, errors.Wrap(err, "failed to list objects from s3")
}
return res, nil
}