-
Notifications
You must be signed in to change notification settings - Fork 0
/
zstd.go
71 lines (59 loc) · 1.33 KB
/
zstd.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
package interceptors
import (
"bytes"
"io"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/klauspost/compress/zstd"
"github.com/linolabx/lino_s3"
)
var Zstd = &lino_s3.Interceptor{
PostGet: func(output *s3.GetObjectOutput) (*s3.GetObjectOutput, error) {
zr, err := zstd.NewReader(output.Body)
if err != nil {
return nil, err
}
output.Body = zr.IOReadCloser()
return output, nil
},
PrePut: func(input *s3.PutObjectInput) (*s3.PutObjectInput, error) {
var buf bytes.Buffer
zz, err := zstd.NewWriter(&buf)
if err != nil {
return nil, err
}
data, err := io.ReadAll(input.Body)
if err != nil {
return nil, err
}
if _, err := zz.Write(data); err != nil {
return nil, err
}
if err := zz.Close(); err != nil {
return nil, err
}
input.Body = bytes.NewReader(buf.Bytes())
return input, nil
},
PreUpload: func(input *s3manager.UploadInput) (*s3manager.UploadInput, error) {
pr, pw := io.Pipe()
go func() {
zw, err := zstd.NewWriter(pw)
if err != nil {
pw.CloseWithError(err)
return
}
if _, err := io.Copy(zw, input.Body); err != nil {
pw.CloseWithError(err)
return
}
if err := zw.Close(); err != nil {
pw.CloseWithError(err)
return
}
pw.Close()
}()
input.Body = pr
return input, nil
},
}