/
s3.go
146 lines (128 loc) · 4.18 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package storage
import (
"context"
"errors"
"io"
"io/fs"
"path/filepath"
"github.com/aws/aws-sdk-go-v2/aws"
awsConfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/rotisserie/eris"
log "github.com/sirupsen/logrus"
"github.com/G-Research/fasttrackml/pkg/api/mlflow/config"
)
// S3StorageName is a s3 storage name.
const (
S3StorageName = "s3"
)
// S3 represents S3 adapter to work with artifacts.
type S3 struct {
client *s3.Client
}
// NewS3 creates new S3 instance.
func NewS3(ctx context.Context, config *config.ServiceConfig) (*S3, error) {
var clientOptions []func(o *s3.Options)
var configOptions []func(*awsConfig.LoadOptions) error
if config.S3EndpointURI != "" {
clientOptions = append(clientOptions, func(o *s3.Options) {
o.UsePathStyle = true
})
configOptions = append(configOptions, awsConfig.WithEndpointResolverWithOptions(
aws.EndpointResolverWithOptionsFunc(
func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == s3.ServiceID {
return aws.Endpoint{
URL: config.S3EndpointURI,
SigningRegion: region,
}, nil
}
return aws.Endpoint{}, eris.Errorf("unknown endpoint requested for the service: %s", service)
},
),
))
}
cfg, err := awsConfig.LoadDefaultConfig(ctx, configOptions...)
if err != nil {
return nil, eris.Wrap(err, "error loading configuration for S3 client")
}
return &S3{
s3.NewFromConfig(cfg, clientOptions...),
}, nil
}
// List implements ArtifactStorageProvider interface.
func (s S3) List(ctx context.Context, artifactURI, path string) ([]ArtifactObject, error) {
// 1. create s3 request input.
bucket, rootPrefix, err := ExtractBucketAndPrefix(artifactURI)
if err != nil {
return nil, eris.Wrap(err, "error extracting bucket and prefix from provided uri")
}
input := s3.ListObjectsV2Input{
Bucket: aws.String(bucket),
Delimiter: aws.String("/"),
}
// 2. process search `path` parameter.
prefix := filepath.Join(rootPrefix, path)
if prefix != "" {
prefix = prefix + "/"
}
input.Prefix = aws.String(prefix)
// 3. read data from s3 storage.
var artifactList []ArtifactObject
paginator := s3.NewListObjectsV2Paginator(s.client, &input)
for paginator.HasMorePages() {
page, err := paginator.NextPage(ctx)
if err != nil {
return nil, eris.Wrap(err, "error getting s3 page objects")
}
log.Debugf("got %d directories from S3 storage for bucket %q and prefix %q", len(page.CommonPrefixes), bucket, prefix)
for _, dir := range page.CommonPrefixes {
relPath, err := filepath.Rel(rootPrefix, *dir.Prefix)
if err != nil {
return nil, eris.Wrapf(err, "error getting relative path for dir: %s", *dir.Prefix)
}
artifactList = append(artifactList, ArtifactObject{
Path: relPath,
Size: 0,
IsDir: true,
})
}
log.Debugf("got %d objects from S3 storage for bucket %q and prefix %q", len(page.Contents), bucket, prefix)
for _, object := range page.Contents {
relPath, err := filepath.Rel(rootPrefix, *object.Key)
if err != nil {
return nil, eris.Wrapf(err, "error getting relative path for object: %s", *object.Key)
}
artifactList = append(artifactList, ArtifactObject{
Path: relPath,
Size: *object.Size,
IsDir: false,
})
}
}
return artifactList, nil
}
// Get returns file content at the storage location.
func (s S3) Get(ctx context.Context, artifactURI, path string) (io.ReadCloser, error) {
// 1. create s3 request input.
bucketName, prefix, err := ExtractBucketAndPrefix(artifactURI)
if err != nil {
return nil, eris.Wrap(err, "error extracting bucket and prefix from provided uri")
}
input := &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(filepath.Join(prefix, path)),
}
// 2. get object from s3 storage.
resp, err := s.client.GetObject(ctx, input)
if err != nil {
// errors.Is is not working for s3 errors, so we need to use errors.As instead.
var s3NoSuchKey *types.NoSuchKey
if errors.As(err, &s3NoSuchKey) {
return nil, eris.Wrap(fs.ErrNotExist, "object does not exist")
}
return nil, eris.Wrap(err, "error getting object")
}
return resp.Body, nil
}