Skip to content

Commit d2bd207

Browse files
Pedro SilvaPedro Silva
authored andcommitted
Added Support to upload to AWS S3
1 parent 273536a commit d2bd207

1 file changed

Lines changed: 105 additions & 3 deletions

File tree

cmd/logshare-cli/main.go

Lines changed: 105 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,14 @@ import (
1414
"github.com/pkg/errors"
1515
"github.com/urfave/cli"
1616
"golang.org/x/net/context"
17+
18+
"github.com/aws/aws-sdk-go/aws"
19+
"github.com/aws/aws-sdk-go/aws/session"
20+
"github.com/aws/aws-sdk-go/service/s3/s3manager"
21+
22+
"sync"
23+
"compress/gzip"
24+
1725
)
1826

1927
// Rev is set on build time and should contain the git commit logshare-cli
@@ -31,11 +39,12 @@ func main() {
3139
app.Flags = flags
3240
app.Version = Rev
3341

34-
conf := &config{}
35-
app.Action = run(conf)
42+
runCtx := RunContext{}
43+
app.Action = run(&runCtx)
3644
if err := app.Run(os.Args); err != nil {
3745
log.Println(err)
3846
}
47+
runCtx.wg.Wait()
3948
}
4049

4150
func setupGoogleStr(projectId string, bucketName string, filename string) (*gcs.Writer, error) {
@@ -59,7 +68,47 @@ func setupGoogleStr(projectId string, bucketName string, filename string) (*gcs.
5968
return obj.NewWriter(gCtx), error
6069
}
6170

62-
func run(conf *config) func(c *cli.Context) error {
71+
func setupAWSStr(runCtx *RunContext, filename string) (*io.PipeWriter, error) {
72+
conf := &runCtx.conf
73+
74+
reader, writer := io.Pipe()
75+
76+
sess, err := session.NewSession(&aws.Config{
77+
Region: aws.String(conf.awsS3Region)},
78+
)
79+
80+
if conf.awsS3Compress {
81+
filename += ".gz"
82+
}
83+
84+
if err != nil {
85+
log.Fatalln("Failed to create session", err)
86+
}
87+
88+
key := filename
89+
if conf.awsS3BucketPath != ""{
90+
key = conf.awsS3BucketPath + "/" + filename
91+
}
92+
93+
runCtx.wg.Add(1)
94+
go func() {
95+
uploader := s3manager.NewUploader(sess)
96+
_, err := uploader.Upload(&s3manager.UploadInput{
97+
Body: reader,
98+
Bucket: aws.String(conf.awsS3Bucket),
99+
Key: aws.String(key),
100+
})
101+
102+
if err != nil {
103+
log.Fatalln("Failed to upload", err)
104+
}
105+
runCtx.wg.Done()
106+
}()
107+
108+
return writer, err
109+
}
110+
func run(runCtx *RunContext) func(c *cli.Context) error {
111+
conf := &runCtx.conf
63112
return func(c *cli.Context) error {
64113
if err := parseFlags(conf, c); err != nil {
65114
cli.ShowAppHelp(c)
@@ -83,13 +132,34 @@ func run(conf *config) func(c *cli.Context) error {
83132
fileName := "cloudflare_els_" + conf.zoneID + "_" + strconv.Itoa(int(time.Now().Unix())) + ".json"
84133

85134
gcsWriter, err := setupGoogleStr(conf.googleProjectId, conf.googleStorageBucket, fileName)
135+
86136
if err != nil {
87137
return err
88138
}
139+
89140
defer gcsWriter.Close()
90141
outputWriter = gcsWriter
91142
}
92143

144+
if conf.awsS3Bucket != "" {
145+
fileName := "cloudflare_els_" + conf.zoneID + "_" + strconv.Itoa(int(time.Now().Unix())) + ".json"
146+
147+
writer, err := setupAWSStr(runCtx, fileName)
148+
149+
if err != nil {
150+
return err
151+
}
152+
153+
defer writer.Close()
154+
155+
if conf.awsS3Compress {
156+
outputWriter = gzip.NewWriter(writer)
157+
}else{
158+
outputWriter = writer
159+
}
160+
161+
}
162+
93163
client, err := logshare.New(
94164
conf.apiKey,
95165
conf.apiEmail,
@@ -134,6 +204,7 @@ func run(conf *config) func(c *cli.Context) error {
134204
}
135205
}
136206

207+
137208
func parseFlags(conf *config, c *cli.Context) error {
138209
conf.apiKey = c.String("api-key")
139210
conf.apiEmail = c.String("api-email")
@@ -149,10 +220,19 @@ func parseFlags(conf *config, c *cli.Context) error {
149220
conf.listFields = c.Bool("list-fields")
150221
conf.googleStorageBucket = c.String("google-storage-bucket")
151222
conf.googleProjectId = c.String("google-project-id")
223+
conf.awsS3Bucket = c.String("aws-s3-bucket")
224+
conf.awsS3BucketPath = c.String("aws-s3-path")
225+
conf.awsS3Region = c.String("aws-s3-region")
226+
conf.awsS3Compress = c.Bool("aws-s3-compress")
152227

153228
return conf.Validate()
154229
}
155230

231+
type RunContext struct {
232+
conf config
233+
wg sync.WaitGroup
234+
}
235+
156236
type config struct {
157237
apiKey string
158238
apiEmail string
@@ -168,6 +248,10 @@ type config struct {
168248
listFields bool
169249
googleStorageBucket string
170250
googleProjectId string
251+
awsS3Bucket string
252+
awsS3BucketPath string
253+
awsS3Region string
254+
awsS3Compress bool
171255
}
172256

173257
func (conf *config) Validate() error {
@@ -255,4 +339,22 @@ var flags = []cli.Flag{
255339
Name: "google-project-id",
256340
Usage: "Project ID of the Google Cloud Storage Bucket to upload logs to",
257341
},
342+
cli.StringFlag{
343+
Name: "aws-s3-bucket",
344+
Usage: "Name of the AWS S3 Bucket to upload logs to",
345+
},
346+
cli.StringFlag{
347+
Name: "aws-s3-path",
348+
Value: "",
349+
Usage: "AWS S3 Path inside bucket to upload logs to",
350+
},
351+
cli.StringFlag{
352+
Name: "aws-s3-region",
353+
Value: "us-west-2",
354+
Usage: "AWS Region of the AWS S3 Bucket to upload logs to (default is us-west-2)",
355+
},
356+
cli.BoolFlag{
357+
Name: "aws-s3-compress",
358+
Usage: "Upload compressed logs to AWS S3. Default false ",
359+
},
258360
}

0 commit comments

Comments
 (0)