-
Notifications
You must be signed in to change notification settings - Fork 342
/
s3.go
259 lines (236 loc) · 6.21 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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package aws
import (
"context"
"errors"
"fmt"
"io"
"sort"
"strings"
"time"
"github.com/Shopify/go-lua"
"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"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
"github.com/treeverse/lakefs/pkg/actions/lua/util"
)
var errDeleteObject = errors.New("delete object failed")
func newS3Client(ctx context.Context) lua.Function {
return func(l *lua.State) int {
accessKeyID := lua.CheckString(l, 1)
secretAccessKey := lua.CheckString(l, 2)
var region string
if !l.IsNone(3) {
region = lua.CheckString(l, 3)
}
var endpoint string
if !l.IsNone(4) {
endpoint = lua.CheckString(l, 4)
}
c := &S3Client{
AccessKeyID: accessKeyID,
SecretAccessKey: secretAccessKey,
Endpoint: endpoint,
Region: region,
ctx: ctx,
}
l.NewTable()
for name, goFn := range functions {
// -1: tbl
l.PushGoFunction(goFn(c))
// -1: fn, -2:tbl
l.SetField(-2, name)
}
return 1
}
}
type S3Client struct {
AccessKeyID string
SecretAccessKey string
Endpoint string
Region string
ctx context.Context
}
func (c *S3Client) client() *s3.Client {
cfg, err := config.LoadDefaultConfig(c.ctx,
config.WithRegion(c.Region),
config.WithCredentialsProvider(credentials.NewStaticCredentialsProvider(c.AccessKeyID, c.SecretAccessKey, "")),
)
if err != nil {
panic(err)
}
return s3.NewFromConfig(cfg, func(o *s3.Options) {
if c.Endpoint != "" {
o.BaseEndpoint = aws.String(c.Endpoint)
}
})
}
var functions = map[string]func(client *S3Client) lua.Function{
"get_object": getObject,
"put_object": putObject,
"list_objects": listObjects,
"delete_object": deleteObject,
"delete_recursive": deleteRecursive,
}
func deleteRecursive(c *S3Client) lua.Function {
return func(l *lua.State) int {
bucketName := lua.CheckString(l, 1)
prefix := lua.CheckString(l, 2)
client := c.client()
input := &s3.ListObjectsV2Input{
Bucket: aws.String(bucketName),
Prefix: aws.String(prefix),
}
var errs error
for {
// list objects to delete and delete them
listObjects, err := client.ListObjectsV2(c.ctx, input)
if err != nil {
lua.Errorf(l, err.Error())
panic("unreachable")
}
deleteInput := &s3.DeleteObjectsInput{
Bucket: &bucketName,
Delete: &types.Delete{},
}
for _, content := range listObjects.Contents {
deleteInput.Delete.Objects = append(deleteInput.Delete.Objects, types.ObjectIdentifier{Key: content.Key})
}
deleteObjects, err := client.DeleteObjects(c.ctx, deleteInput)
if err != nil {
errs = errors.Join(errs, err)
break
}
for _, deleteError := range deleteObjects.Errors {
errDel := fmt.Errorf("%w '%s', %s",
errDeleteObject, aws.ToString(deleteError.Key), aws.ToString(deleteError.Message))
errs = errors.Join(errs, errDel)
}
if !listObjects.IsTruncated {
break
}
input.ContinuationToken = listObjects.NextContinuationToken
}
if errs != nil {
lua.Errorf(l, errs.Error())
panic("unreachable")
}
return 0
}
}
func getObject(c *S3Client) lua.Function {
return func(l *lua.State) int {
client := c.client()
key := lua.CheckString(l, 2)
bucket := lua.CheckString(l, 1)
resp, err := client.GetObject(c.ctx, &s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
var (
noSuchBucket *types.NoSuchBucket
noSuchKey *types.NoSuchKey
)
if errors.As(err, &noSuchBucket) || errors.As(err, &noSuchKey) {
l.PushString("")
l.PushBoolean(false) // exists
return 2
}
lua.Errorf(l, err.Error())
panic("unreachable")
}
data, err := io.ReadAll(resp.Body)
if err != nil {
lua.Errorf(l, err.Error())
panic("unreachable")
}
l.PushString(string(data))
l.PushBoolean(true) // exists
return 2
}
}
func putObject(c *S3Client) lua.Function {
return func(l *lua.State) int {
client := c.client()
buf := strings.NewReader(lua.CheckString(l, 3))
_, err := client.PutObject(c.ctx, &s3.PutObjectInput{
Body: buf,
Bucket: aws.String(lua.CheckString(l, 1)),
Key: aws.String(lua.CheckString(l, 2)),
})
if err != nil {
lua.Errorf(l, err.Error())
panic("unreachable")
}
return 0
}
}
func deleteObject(c *S3Client) lua.Function {
return func(l *lua.State) int {
client := c.client()
_, err := client.DeleteObject(c.ctx, &s3.DeleteObjectInput{
Bucket: aws.String(lua.CheckString(l, 1)),
Key: aws.String(lua.CheckString(l, 2)),
})
if err != nil {
lua.Errorf(l, err.Error())
panic("unreachable")
}
return 0
}
}
func listObjects(c *S3Client) lua.Function {
return func(l *lua.State) int {
client := c.client()
var prefix, delimiter, continuationToken *string
if !l.IsNone(2) {
prefix = aws.String(lua.CheckString(l, 2))
}
if !l.IsNone(3) {
continuationToken = aws.String(lua.CheckString(l, 3))
}
if !l.IsNone(4) {
delimiter = aws.String(lua.CheckString(l, 4))
} else {
delimiter = aws.String("/")
}
resp, err := client.ListObjectsV2(c.ctx, &s3.ListObjectsV2Input{
Bucket: aws.String(lua.CheckString(l, 1)),
ContinuationToken: continuationToken,
Delimiter: delimiter,
Prefix: prefix,
})
if err != nil {
lua.Errorf(l, err.Error())
panic("unreachable")
}
results := make([]map[string]interface{}, 0)
for _, prefix := range resp.CommonPrefixes {
results = append(results, map[string]interface{}{
"key": *prefix.Prefix,
"type": "prefix",
})
}
for _, obj := range resp.Contents {
results = append(results, map[string]interface{}{
"key": *obj.Key,
"type": "object",
"etag": *obj.ETag,
"size": obj.Size,
"last_modified": obj.LastModified.Format(time.RFC3339),
})
}
// sort it
sort.Slice(results, func(i, j int) bool {
return results[i]["key"].(string) > results[j]["key"].(string)
})
response := map[string]interface{}{
"is_truncated": resp.IsTruncated,
"next_continuation_token": aws.ToString(resp.NextContinuationToken),
"results": results,
}
return util.DeepPush(l, response)
}
}