forked from go-xiaohei/pugo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws_s3.go
123 lines (112 loc) · 3.01 KB
/
aws_s3.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
package deploy
import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
"os"
"path/filepath"
"time"
"github.com/Unknwon/com"
"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/urfave/cli"
"gopkg.in/inconshreveable/log15.v2"
)
type AwsS3 struct {
Local string
AccessKey string
SecretKey string
Bucket string
Region string
}
func (a *AwsS3) Command() cli.Command {
return cli.Command{
Name: "aws-s3",
Usage: "deploy via aws-sdk to Amazon Storage Service",
Flags: []cli.Flag{
cli.StringFlag{Name: "local", Value: "dest", Usage: "local website directory"},
cli.StringFlag{Name: "ak", Usage: "accesss key"},
cli.StringFlag{Name: "sk", Usage: "secret key"},
cli.StringFlag{Name: "bucket", Usage: "storage bucket name"},
cli.StringFlag{Name: "region", Usage: "storage bucket region"},
},
Action: func(ctx *cli.Context) {
t := time.Now()
a2, err := a.Create(ctx)
if err != nil {
log15.Error("AWS|Fail|%s", err.Error())
return
}
if err = a2.Do(); err != nil {
log15.Error("AWS|Fail|%s", err.Error())
return
}
log15.Info("AWS|Finish|%s", time.Since(t))
},
}
}
func (a *AwsS3) String() string {
return "AWS-S3"
}
func (a *AwsS3) Create(ctx *cli.Context) (Method, error) {
a2 := &AwsS3{
Local: ctx.String("local"),
AccessKey: ctx.String("ak"),
SecretKey: ctx.String("sk"),
Bucket: ctx.String("bucket"),
Region: ctx.String("region"),
}
if !com.IsDir(a2.Local) {
return nil, fmt.Errorf("directory '%s' is not existed", a2.Local)
}
if a2.AccessKey == "" || a2.SecretKey == "" {
return nil, fmt.Errorf("S3's accessKey or secretKey is empty")
}
if a2.Bucket == "" || a2.Region == "" {
return nil, fmt.Errorf("S3's Bucket or Region is not setted")
}
return a2, nil
}
func (a *AwsS3) Do() error {
creds := credentials.NewStaticCredentials(a.AccessKey, a.SecretKey, "")
_, err := creds.Get()
if err != nil {
return err
}
cfg := aws.NewConfig().WithRegion(a.Region).WithCredentials(creds)
s3client := s3.New(session.New(), cfg)
log15.Info("AWS|Bucket|%s", a.Region)
err = filepath.Walk(a.Local, func(path string, info os.FileInfo, err error) error {
if info.IsDir() {
return nil
}
rel, _ := filepath.Rel(a.Local, path)
rel = filepath.ToSlash(rel)
fileData, err := ioutil.ReadFile(path)
if err != nil {
return err
}
size := info.Size()
fileType := http.DetectContentType(fileData)
params := &s3.PutObjectInput{
Bucket: aws.String(a.Bucket), // required
Key: aws.String(rel), // required
ACL: aws.String("public-read"),
Body: bytes.NewReader(fileData),
ContentLength: aws.Int64(size),
ContentType: aws.String(fileType),
Metadata: map[string]*string{
"Key": aws.String(filepath.Base(rel)), //required
},
}
if _, err = s3client.PutObject(params); err != nil {
return err
}
log15.Info("AWS|Upload|%s", rel)
return nil
})
return err
}