This repository has been archived by the owner on Aug 15, 2023. It is now read-only.
/
client.go
60 lines (50 loc) · 1.96 KB
/
client.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
package s3
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/request"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3iface"
awsbase "github.com/hashicorp/aws-sdk-go-base"
)
// Client is an abstraction layer for AWS S3 API.
// It is intended to be replaced with a mock for testing.
type Client interface {
// PutObjectWithContext puts a file to S3.
PutObjectWithContext(ctx aws.Context, input *s3.PutObjectInput, opts ...request.Option) (*s3.PutObjectOutput, error)
// GetObjectWithContext gets a file from S3.
GetObjectWithContext(ctx aws.Context, input *s3.GetObjectInput, opts ...request.Option) (*s3.GetObjectOutput, error)
}
// client is a real implementation of the Client.
type client struct {
s3api s3iface.S3API
}
// newClient returns a new instance of Client.
func newClient(config *Config) (Client, error) {
cfg := &awsbase.Config{
AccessKey: config.AccessKey,
AssumeRoleARN: config.RoleARN,
Profile: config.Profile,
Region: config.Region,
SecretKey: config.SecretKey,
SkipCredsValidation: config.SkipCredentialsValidation,
SkipMetadataApiCheck: config.SkipMetadataAPICheck,
}
sess, err := awsbase.GetSession(cfg)
if err != nil {
return nil, fmt.Errorf("failed to new s3 client: %s", err)
}
client := s3.New(sess.Copy(&aws.Config{
Endpoint: aws.String(config.Endpoint),
S3ForcePathStyle: aws.Bool(config.ForcePathStyle),
}))
return client, nil
}
// PutObjectWithContext puts a file to S3.
func (c *client) PutObjectWithContext(ctx aws.Context, input *s3.PutObjectInput, opts ...request.Option) (*s3.PutObjectOutput, error) {
return c.s3api.PutObjectWithContext(ctx, input, opts...)
}
// GetObjectWithContext gets a file from S3.
func (c *client) GetObjectWithContext(ctx aws.Context, input *s3.GetObjectInput, opts ...request.Option) (*s3.GetObjectOutput, error) {
return c.s3api.GetObjectWithContext(ctx, input, opts...)
}