-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
114 lines (94 loc) · 2.63 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
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
/*
Copyright 2023 EscherCloud.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package s3
import (
"context"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/credentials"
"github.com/aws/aws-sdk-go-v2/service/s3"
"io"
)
type S3 struct {
Bucket string
Client *s3.Client
}
func New(endpoint, accessKey, secretKey, bucket, region string) (*S3, error) {
const defaultRegion = "us-east-1"
r := defaultRegion
if region != "" {
r = region
}
resolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...any) (aws.Endpoint, error) {
return aws.Endpoint{
PartitionID: "aws",
URL: endpoint,
SigningRegion: r,
HostnameImmutable: true,
}, nil
})
ctx := context.Background()
cfg, err := config.LoadDefaultConfig(ctx,
config.WithRegion(r),
config.WithEndpointResolverWithOptions(resolver),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(accessKey, secretKey, "")),
)
if err != nil {
return nil, err
}
return &S3{
Bucket: bucket,
Client: s3.NewFromConfig(cfg),
}, nil
}
// Fetch Downloads a file from an S3 bucket and returns its contents as a byte array.
func (s *S3) Fetch(fileName string) ([]byte, error) {
params := &s3.GetObjectInput{
Bucket: &s.Bucket,
Key: &fileName,
}
obj, err := s.Client.GetObject(context.Background(), params)
if err != nil {
return nil, err
}
return io.ReadAll(obj.Body)
}
// Put Pushes a file to an S3 bucket.
func (s *S3) Put(contentType, key string, body io.ReadSeeker) error {
params := &s3.PutObjectInput{
Bucket: &s.Bucket,
Key: &key,
ContentType: &contentType,
Body: body,
}
_, err := s.Client.PutObject(context.Background(), params)
if err != nil {
return err
}
return nil
}
// List will list the contents of a bucket
func (s *S3) List() ([]string, error) {
params := &s3.ListObjectsInput{
Bucket: &s.Bucket,
}
obj, err := s.Client.ListObjects(context.Background(), params)
if err != nil {
return nil, err
}
contents := []string{}
for _, v := range obj.Contents {
contents = append(contents, *v.Key)
}
return contents, nil
}