forked from jhunt/go-s3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
93 lines (77 loc) · 1.79 KB
/
main.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
package main
import (
"fmt"
"io"
"os"
"github.com/jhunt/go-s3"
)
func main() {
aki := os.Getenv("S3_AKI")
if aki == "" {
fmt.Fprintf(os.Stderr, "!!! no S3_AKI env var set...\n")
os.Exit(1)
}
key := os.Getenv("S3_KEY")
if key == "" {
fmt.Fprintf(os.Stderr, "!!! no S3_KEY env var set...\n")
os.Exit(1)
}
bkt := os.Getenv("S3_BUCKET")
if bkt == "" {
fmt.Fprintf(os.Stderr, "!!! no S3_BUCKET env var set...\n")
os.Exit(1)
}
reg := os.Getenv("S3_REGION")
if reg == "" {
reg = "us-east-1"
}
sigv := 4
if os.Getenv("S3_SIGV2") != "" {
sigv = 2
}
if len(os.Args) != 2 {
fmt.Fprintf(os.Stderr, "USAGE: s3 path/in/bucket <file >out\n")
os.Exit(1)
}
path := os.Args[1]
c, err := s3.NewClient(&s3.Client{
AccessKeyID: aki,
SecretAccessKey: key,
Region: reg,
Bucket: bkt,
SignatureVersion: sigv,
UsePathBuckets: os.Getenv("S3_PATH_BUCKET") != "",
})
if err != nil {
fmt.Fprintf(os.Stderr, "!!! unable to configure s3 client: %s\n", err)
os.Exit(1)
}
u, err := c.NewUpload(path)
if err != nil {
fmt.Fprintf(os.Stderr, "!!! unable to start multipart upload: %s\n", err)
os.Exit(1)
}
n, err := u.Stream(os.Stdin, 5*1024*1024*1024)
if err != nil {
fmt.Fprintf(os.Stderr, "!!! unable to stream <stdin> in 5m parts: %s\n", err)
os.Exit(1)
}
err = u.Done()
if err != nil {
fmt.Fprintf(os.Stderr, "!!! unable to complete multipart upload: %s\n", err)
os.Exit(1)
}
fmt.Fprintf(os.Stderr, "wrote %d bytes\n", n)
out, err := c.Get(path)
if err != nil {
fmt.Fprintf(os.Stderr, "!!! unable to retrieve our uploaded file: %s\n", err)
os.Exit(1)
}
io.Copy(os.Stdout, out)
err = c.Delete(path)
if err != nil {
fmt.Fprintf(os.Stderr, "!!! unable to remove our uploaded file: %s\n", err)
os.Exit(1)
}
os.Exit(0)
}