forked from rlmcpherson/s3gof3r
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get.go
75 lines (66 loc) · 1.62 KB
/
get.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
package main
import (
"fmt"
"io"
"log"
"os"
"github.com/rlmcpherson/s3gof3r"
)
type getOpts struct {
Key string `long:"key" short:"k" description:"S3 object key" required:"true" no-ini:"true"`
Bucket string `long:"bucket" short:"b" description:"S3 bucket" required:"true" no-ini:"true"`
Path string `short:"p" long:"path" description:"Path to file. Defaults to standard output for streaming." no-ini:"true"`
DataOpts
CommonOpts
VersionID string `short:"v" long:"versionId" description:"Version ID of the object. Incompatible with md5 check (use --no-md5)." no-ini:"true"`
}
var get getOpts
func (get *getOpts) Execute(args []string) (err error) {
conf := new(s3gof3r.Config)
*conf = *s3gof3r.DefaultConfig
k, err := getAWSKeys()
if err != nil {
return
}
s3 := s3gof3r.New(get.EndPoint, k)
b := s3.Bucket(get.Bucket)
conf.Concurrency = get.Concurrency
if get.NoSSL {
conf.Scheme = "http"
}
conf.PartSize = get.PartSize
conf.Md5Check = !get.NoMd5
s3gof3r.SetLogger(os.Stderr, "", log.LstdFlags, get.Debug)
if get.VersionID != "" {
get.Key = fmt.Sprintf("%s?versionId=%s", get.Key, get.VersionID)
}
w, err := os.Create(get.Path)
if err != nil {
if get.Path == "" {
w = os.Stdout
} else {
return
}
}
defer w.Close()
r, header, err := b.GetReader(get.Key, conf)
if err != nil {
return
}
if _, err = io.Copy(w, r); err != nil {
return
}
if err = r.Close(); err != nil {
return
}
if get.Debug {
log.Println("Headers: ", header)
}
return
}
func init() {
_, err := parser.AddCommand("get", "download from S3", "get (download) object from S3", &get)
if err != nil {
log.Fatal(err)
}
}