-
Notifications
You must be signed in to change notification settings - Fork 13
/
utils.go
450 lines (397 loc) · 9.92 KB
/
utils.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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package s3tar
import (
"archive/tar"
"bytes"
"context"
"crypto/md5"
"crypto/rand"
"encoding/hex"
"fmt"
"io"
"log"
"net/url"
"os"
"regexp"
"strconv"
"strings"
"time"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/s3/types"
)
type contextKey string
const (
contextKeyS3Client = contextKey("s3-client")
)
var (
extractS3 = regexp.MustCompile(`s3://(.[^/]*)/?(.*)`)
)
// S3TarS3Options options to create an archive
type S3TarS3Options struct {
SrcManifest string
SkipManifestHeader bool
SrcBucket string
SrcPrefix string
SrcKey string
DstBucket string
DstPrefix string
DstKey string
Threads int
DeleteSource bool
Region string
EndpointUrl string
ExternalToc string
tarFormat tar.Format
storageClass types.StorageClass
extractPrefix string
ConcatInMemory bool
UrlDecode bool
UserMaxPartSize int64
ObjectTags types.Tagging
KMSKeyID string
SSEAlgo types.ServerSideEncryption
PreservePOSIXMetadata bool
}
func TagsToUrlEncodedString(tagging types.Tagging) string {
vals := url.Values{}
for _, x := range tagging.TagSet {
vals.Add(*x.Key, *x.Value)
}
return vals.Encode()
}
func (o *S3TarS3Options) Copy() S3TarS3Options {
to := *o
return to
}
func findMinMaxPartRange(objectSize int64) (int64, int64, int64) {
const (
KB int64 = 1024
partsLimit int64 = 10000
partSizeMin int64 = KB * KB * 5
partSizeMax int64 = KB * KB * KB * 5
// optimalSize = 1024 * 1024 * 16
)
// partSizeMin = 1000 * 1000 * 5
// partSizeMax = 5e+9
// curSize = 5e+12 #5TB
curSize := objectSize
nPartsMax := partsLimit
var nPartsMaxSize int64 = 0
for {
nPartsMaxSize = curSize / nPartsMax
if nPartsMaxSize < partSizeMin {
nPartsMax = nPartsMax - 1
continue
}
break
}
var nPartsMin int64 = 1
var nPartsMinSize int64 = 0
for {
nPartsMinSize = curSize / nPartsMin
if nPartsMinSize > partSizeMax {
nPartsMin += 1
continue
}
break
}
mid := nPartsMax / 2
return nPartsMin, nPartsMax, mid
}
type PartsMessage struct {
Parts []*S3Obj
PartNum int
}
func NewS3Obj() *S3Obj {
now := time.Now()
return &S3Obj{
Object: types.Object{
Key: aws.String(""),
ETag: aws.String(""),
LastModified: &now,
},
}
}
func NewS3ObjOptions(options ...func(*S3Obj)) *S3Obj {
now := time.Now()
obj := &S3Obj{
Object: types.Object{
Key: aws.String(""),
ETag: aws.String(""),
LastModified: &now,
},
}
for _, o := range options {
o(obj)
}
return obj
}
func WithBucketAndKey(bucket, key string) func(*S3Obj) {
return func(o *S3Obj) {
o.Bucket = bucket
o.Key = &key
}
}
func WithSize(size int64) func(*S3Obj) {
return func(o *S3Obj) {
o.Size = &size
}
}
func WithETag(etag string) func(*S3Obj) {
return func(o *S3Obj) {
o.ETag = &etag
}
}
func NewS3ObjFromObject(o types.Object) *S3Obj {
return &S3Obj{Object: o}
}
func StringToInt64(s string) (int64, error) {
i, err := strconv.ParseInt(s, 10, 64)
if err != nil {
return 0, err
}
return i, nil
}
type S3Obj struct {
types.Object
Bucket string
PartNum int
Data []byte
NoHeaderRequired bool
}
func (s *S3Obj) AddData(data []byte) {
etag := fmt.Sprintf("%x", md5.Sum(data))
s.Data = data
s.Size = aws.Int64(int64(len(data)))
s.ETag = &etag
}
type VirtualArchive []*S3Obj
type byPartNum []*S3Obj
func (a byPartNum) Len() int { return len(a) }
func (a byPartNum) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byPartNum) Less(i, j int) bool { return a[i].PartNum < a[j].PartNum }
type Index struct {
Start int
End int
Size int
}
func findPadding(offset int64) (n int64) {
return -offset & (blockSize - 1)
}
type Logger struct {
Level int
}
// ExtractBucketAndPath helper function to extract bucket and key from s3://bucket/prefix/key URLs
func ExtractBucketAndPath(s3url string) (bucket string, path string) {
parts := extractS3.FindAllStringSubmatch(s3url, -1)
if len(parts) > 0 && len(parts[0]) > 2 {
bucket = parts[0][1]
path = parts[0][2]
}
return
}
func filter[T any](ss []T, test func(T) bool) (ret []T) {
for _, s := range ss {
if test(s) {
ret = append(ret, s)
}
}
return
}
func removeDirs(object types.Object) bool {
name := *object.Key
if string(name[len(name)-1]) == "/" {
return false
}
return true
}
func ListAllObjects(ctx context.Context, client *s3.Client, Bucket, Prefix string, filterFns ...func(types.Object) bool) ([]*S3Obj, int64, error) {
input := &s3.ListObjectsV2Input{
Bucket: &Bucket,
Prefix: &Prefix,
}
var accum int64
ctr := 1
var list []*S3Obj
var defaultFilter []func(types.Object) bool
defaultFilter = append(defaultFilter, removeDirs)
allFilters := append(defaultFilter, filterFns...)
p := s3.NewListObjectsV2Paginator(client, input)
for {
if !p.HasMorePages() {
break
}
output, err := p.NextPage(ctx)
if err != nil {
log.Print(err.Error())
return list, accum, err
}
contents := output.Contents
if len(allFilters) > 0 {
for _, tf := range allFilters {
contents = filter(contents, tf)
}
}
for _, o := range contents {
list = append(list, &S3Obj{
Object: o,
Bucket: Bucket,
PartNum: ctr,
})
ctr += 1
accum += estimateObjectSize(*o.Size)
}
}
return list, accum, nil
}
// estimate the object size including header and padding
func estimateObjectSize(size int64) int64 {
pad := findPadding(size)
return int64(paxTarHeaderSize) + size + pad
}
func BreakUpList(objectList []*S3Obj, limitSize int64) [][]*S3Obj {
var list [][]*S3Obj
var currentList []*S3Obj
var accum int64 = 0
for i := 0; i < len(objectList); i++ {
currentObjectSize := estimateObjectSize(*objectList[i].Size)
if accum+currentObjectSize < limitSize {
currentList = append(currentList, objectList[i])
accum += currentObjectSize
} else {
list = append(list, currentList)
currentList = append([]*S3Obj{}, objectList[i])
accum = currentObjectSize
}
}
if len(currentList) > 0 {
list = append(list, currentList)
}
return list
}
func putObject(ctx context.Context, svc *s3.Client, bucket, key string, data []byte) (*s3.PutObjectOutput, error) {
input := &s3.PutObjectInput{
Bucket: &bucket,
Key: &key,
Body: bytes.NewReader(data),
ContentLength: aws.Int64(int64(len(data))),
}
return svc.PutObject(ctx, input)
}
func getObject(ctx context.Context, svc *s3.Client, bucket, key string) (io.ReadCloser, error) {
return getObjectRange(ctx, svc, bucket, key, 0, 0)
}
func getObjectRange(ctx context.Context, svc *s3.Client, bucket, key string, start, end int64) (io.ReadCloser, error) {
params := &s3.GetObjectInput{
Key: &key,
Bucket: &bucket,
}
if end != 0 {
byteRange := fmt.Sprintf("bytes=%d-%d", start, end)
params.Range = &byteRange
}
output, err := svc.GetObject(ctx, params)
if err != nil {
return nil, err
}
return output.Body, nil
}
func loadFile(ctx context.Context, svc *s3.Client, path string) (io.ReadCloser, error) {
if strings.Contains(path, "s3://") {
bucket, key := ExtractBucketAndPath(path)
return getObject(ctx, svc, bucket, key)
} else {
return os.Open(path)
}
}
// DeleteAllMultiparts helper function to clear ALL MultipartUploads in a bucket. This will delete all incomplete (or in progress) MPUs for a bucket.
func DeleteAllMultiparts(client *s3.Client, bucket string) error {
output, err := client.ListMultipartUploads(context.TODO(), &s3.ListMultipartUploadsInput{Bucket: &bucket})
if err != nil {
return err
}
for _, upload := range output.Uploads {
log.Printf("Aborting %s", *upload.UploadId)
_, err := client.AbortMultipartUpload(context.TODO(), &s3.AbortMultipartUploadInput{
Bucket: aws.String(bucket),
Key: upload.Key,
UploadId: upload.UploadId,
})
if err != nil {
Infof(context.Background(), err.Error())
return err
}
// log.Printf("AbortedMultiUpload ok %s", r)
}
return nil
}
func _deleteObjectList(ctx context.Context, client *s3.Client, opts *S3TarS3Options, objectList []*S3Obj) error {
objects := make([]types.ObjectIdentifier, len(objectList))
for i := 0; i < len(objectList); i++ {
objects[i] = types.ObjectIdentifier{
Key: objectList[i].Key,
}
}
params := &s3.DeleteObjectsInput{
Bucket: &objectList[0].Bucket,
Delete: &types.Delete{
Quiet: aws.Bool(true),
Objects: objects,
},
}
response, err := client.DeleteObjects(ctx, params)
if err != nil {
return err
}
if len(response.Errors) > 0 {
Infof(ctx, "Error deleting objects")
return err
}
return nil
}
func deleteObjectList(ctx context.Context, svc *s3.Client, opts *S3TarS3Options, objectList []*S3Obj) error {
batch := 1000
for i := 0; i < len(objectList); i += batch {
start := i
end := i + batch
if end >= len(objectList) {
end = len(objectList)
}
part := objectList[start:end]
err := _deleteObjectList(ctx, svc, opts, part)
if err != nil {
return err
}
}
return nil
}
func randomHex(n int) (string, error) {
bytes := make([]byte, n)
if _, err := rand.Read(bytes); err != nil {
return "", err
}
return hex.EncodeToString(bytes), nil
}
func formatBytes(contentLength int64) string {
if contentLength < 0 {
return "Invalid size"
}
// Define SI unit symbols
units := []string{"Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB"}
// Convert to float64 for calculation
size := float64(contentLength)
// Determine the appropriate unit
unitIndex := 0
for size >= 1024 && unitIndex < len(units)-1 {
size /= 1024
unitIndex++
}
// Format the result with two decimal places
msg := fmt.Sprintf("%.4f %s", size, units[unitIndex])
if units[unitIndex] == "Bytes" {
msg = fmt.Sprintf("%.0f %s", size, units[unitIndex])
}
return msg
}