-
Notifications
You must be signed in to change notification settings - Fork 20
/
compressor.go
88 lines (74 loc) · 1.57 KB
/
compressor.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
package main
import (
"bufio"
"compress/gzip"
"flag"
"io"
"log"
"os"
minio "github.com/minio/minio-go/v6"
)
var (
endpoint = os.Getenv("ENDPOINT")
endpointSSL = os.Getenv("ENDPOINT_SSL")
accessKeyID = os.Getenv("ACCESS_KEY_ID")
accessKeySecret = os.Getenv("ACCESS_KEY_SECRET")
)
func main() {
var (
fmBucket = flag.String("f", "", "From bucket.")
toBucket = flag.String("t", "", "To bucket.")
fmObjKey = flag.String("k", "", "From key.")
)
flag.Parse()
useSSL := true
if endpointSSL == "false" {
useSSL = false
}
mc, err := minio.New(
endpoint, accessKeyID,
accessKeySecret, useSSL)
if err != nil {
log.Fatalln(err)
}
obj, err := mc.GetObject(
*fmBucket,
*fmObjKey,
minio.GetObjectOptions{},
)
if err != nil {
log.Fatalln(err)
}
log.Printf("Starting download stream %s/%s.",
*fmBucket,
*fmObjKey)
// synchronous in-memory pipe
pipeR, pipeW := io.Pipe()
// reads from object, writes to pipe
bufIn := bufio.NewReader(obj)
// gzip buffers to memory and flushes on close
gzW, err := gzip.NewWriterLevel(pipeW, 3)
if err != nil {
log.Fatalln(err)
}
go func() {
log.Printf("Compress and stream.")
n, err := bufIn.WriteTo(gzW)
if err != nil {
log.Fatalln(err)
}
gzW.Close()
pipeW.Close()
log.Printf("Compressed: %d bytes", n)
}()
// data will not be sent until gzW.Close() and
// the gzip buffer flushes
log.Print("BEGIN PutObject")
_, err = mc.PutObject(
*toBucket, *fmObjKey+".gz",
pipeR, -1, minio.PutObjectOptions{})
if err != nil {
log.Fatalln(err)
}
log.Print("COMPLETE PutObject")
}