-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
65 lines (57 loc) · 1.43 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
package main
//GOOS=linux GOARCH=amd64 go build -v --tags netgo -ldflags '-s -w -extldflags "-static"' -o s3get main.go
//tar -czf s3get.tgz s3get
import (
"fmt"
"os"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
var (
s3AK = os.Getenv("GOCD_S3_AK")
s3SK = os.Getenv("GOCD_S3_SK")
s3Endpoint = os.Getenv("GOCD_S3_ENDPOINT")
s3Bucket = os.Getenv("GOCD_S3_BUCKET")
s3Region = os.Getenv("GOCD_S3_REGION")
)
func usage() {
fmt.Println("Usage: s3get S3_FILENAME LOCAL_FILENAME")
}
func main() {
if len(os.Args) < 3 {
usage()
os.Exit(1)
}
s3Filename := os.Args[1]
localFilename := os.Args[2]
if len(s3Filename) == 0 || len(localFilename) == 0 {
usage()
os.Exit(1)
}
sess, _ := session.NewSession(&aws.Config{
Credentials: credentials.NewStaticCredentials(s3AK, s3SK, ""),
Region: aws.String(s3Region),
Endpoint: aws.String(s3Endpoint),
DisableSSL: aws.Bool(true),
S3ForcePathStyle: aws.Bool(true),
},
)
downloader := s3manager.NewDownloader(sess)
file, err := os.Create(localFilename)
if err != nil {
fmt.Println(err)
return
}
_, err = downloader.Download(file,
&s3.GetObjectInput{
Bucket: aws.String(s3Bucket),
Key: aws.String(s3Filename),
})
if err != nil {
fmt.Println(err)
return
}
}