-
Notifications
You must be signed in to change notification settings - Fork 0
/
app_test.go
executable file
·103 lines (87 loc) · 2.64 KB
/
app_test.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
/*
https://www.youtube.com/watch?v=iOGIKG3EptI
https://github.com/awslabs/aws-go-wordfreq-sample/blob/master/cmd/uploads3/main.go
https://docs.aws.amazon.com/sdk-for-go/api/aws/
- first configure your aws credentials run: aws configure
- go get -u github.com/aws/aws-sdk-go/aws
- login to UI web aws s3 interface
- go to S3 service
- create a Bucket called `b3bas-up` in the desired region (default region: `ap-southeast-1`)
- run: go run app.go b3bas-up [file_name]
- run with binary:
make build
./bin/b3bas-up [bucket_name] [file_name]
*/
package main
import (
"bytes"
"fmt"
"io"
"os"
"path/filepath"
"github.com/dimiro1/banner"
_ "github.com/dimiro1/banner/autoload"
colorable "github.com/mattn/go-colorable"
"github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)
var (
TEST_AWS_ACCESS_KEY string = os.Getenv("AWS_ACCESS_KEY")
TEST_AWS_SECRET_KEY string = os.Getenv("AWS_SECRET_KEY")
TEST_AWS_TOKEN string = os.Getenv("AWS_TOKEN")
TEST_S3_REGION string = "ap-southeast-1"
TEST_S3_BUCKET string = "b3bas-up"
)
const B3BAS_LOG_TEST = "/var/log/b3bas-up/b3bas-up.log"
func InitLogoTest() {
isEnabled := true
isColorEnabled := true
banner.Init(colorable.NewColorableStdout(), isEnabled, isColorEnabled, bytes.NewBufferString(" B3BAS Golang Framework {{ .AnsiColor.Green }}(Running){{ .AnsiColor.Default }}\n"))
}
func SaveLogTest() {
// create the logger
logger := logrus.New()
logger.Formatter = &logrus.JSONFormatter{}
fName, err := os.OpenFile(B3BAS_LOG_TEST, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0755)
if err != nil {
logger.Fatal(err)
}
defer fName.Close()
// multiwriter simultaneously
logger.SetOutput(io.MultiWriter(os.Stdout, fName))
println(" Log file saved to:", B3BAS_LOG_TEST)
}
func UploadTest() {
if len(os.Args) != 3 {
fmt.Printf("usage: %s <bucket> <filename>\n", filepath.Base(os.Args[0]))
os.Exit(1)
}
bucket := os.Args[1]
if bucket == "" {
bucket = TEST_S3_BUCKET
}
filename := os.Args[2]
file, err := os.Open(filename)
if err != nil {
fmt.Println("Failed to open file", filename, err)
os.Exit(1)
}
defer file.Close()
//select Region to use.
conf := aws.Config{Region: aws.String(TEST_S3_REGION)}
sess := session.New(&conf)
svc := s3manager.NewUploader(sess)
fmt.Println("Uploading file to S3...")
result, err := svc.Upload(&s3manager.UploadInput{
Bucket: aws.String(bucket),
Key: aws.String(filepath.Base(filename)),
Body: file,
})
if err != nil {
fmt.Println("error", err)
os.Exit(1)
}
fmt.Printf("Successfully uploaded %s to %s\n", filename, result.Location)
}