-
Notifications
You must be signed in to change notification settings - Fork 1
/
s3.go
63 lines (51 loc) · 1.51 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
package s3
import (
"context"
"os"
"github.com/alexfalkowski/go-service/env"
"github.com/alexfalkowski/go-service/transport/http"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
endpoints "github.com/aws/smithy-go/endpoints"
"go.opentelemetry.io/otel/metric"
"go.opentelemetry.io/otel/trace"
"go.uber.org/fx"
"go.uber.org/zap"
)
// ConfigParams for S3.
type ClientParams struct {
fx.In
Config *http.Config
Logger *zap.Logger
Tracer trace.Tracer
Meter metric.Meter
UserAgent env.UserAgent
}
// NewClient for S3.
func NewClient(params ClientParams) (*s3.Client, error) {
client := http.NewClient(
http.WithClientLogger(params.Logger), http.WithClientTracer(params.Tracer),
http.WithClientMetrics(params.Meter), http.WithClientUserAgent(params.UserAgent),
)
ctx := context.Background()
opts := []func(*config.LoadOptions) error{
config.WithHTTPClient(client),
config.WithRetryMaxAttempts(int(params.Config.Retry.Attempts)),
}
r := &resolver{EndpointResolverV2: s3.NewDefaultEndpointResolverV2()}
cfg, err := config.LoadDefaultConfig(ctx, opts...)
cl := s3.NewFromConfig(cfg, s3.WithEndpointResolverV2(r), func(o *s3.Options) {
o.UsePathStyle = true
})
return cl, err
}
type resolver struct {
s3.EndpointResolverV2
}
func (r *resolver) ResolveEndpoint(ctx context.Context, params s3.EndpointParameters) (endpoints.Endpoint, error) {
u := os.Getenv("AWS_URL")
if u != "" {
params.Endpoint = &u
}
return r.EndpointResolverV2.ResolveEndpoint(ctx, params)
}