-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
185 lines (154 loc) · 4.24 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
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
package main
import (
"bytes"
"fmt"
"os"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"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/google/uuid"
"github.com/sirupsen/logrus"
)
func main() {
const (
S3 = "s3"
DefaultRegion = "default"
)
accessKey := os.Getenv("S3_ACCESS_KEY_1")
secretKey := os.Getenv("S3_SECRET_KEY_1")
bucketName := os.Getenv(("S3_BUCKET_NAME_1"))
accessKey2 := os.Getenv("S3_ACCESS_KEY_2")
secretKey2 := os.Getenv("S3_SECRET_KEY_2")
bucketNameTwo := os.Getenv(("S3_BUCKET_NAME_2"))
endpointStr := os.Getenv("S3_ENDPOINT")
regionStr := os.Getenv("S3_REGION")
for {
creds := credentials.NewStaticCredentials(accessKey, secretKey, "")
_, err := creds.Get()
if err != nil {
logrus.Errorf("bad credentials: %s", err)
}
cfg := aws.NewConfig().WithEndpoint(endpointStr).WithDisableSSL(true).WithCredentials(creds).WithS3ForcePathStyle(true)
if len(regionStr) > 0 {
cfg = cfg.WithRegion(regionStr)
} else {
cfg = cfg.WithRegion(DefaultRegion)
}
svc := s3.New(session.New(), cfg)
objName := uuid.New().String()
fmt.Printf("--- PUT OBJECT %s IN BUCKET %s ---\n", objName, bucketName)
putObject(svc, bucketName, objName)
fmt.Printf("--- GET OBJECT %s IN BUCKET %s ---\n", objName, bucketName)
getObject(svc, bucketName, objName)
fmt.Printf("--- DONE ---\n")
fmt.Printf("\n\n")
creds = credentials.NewStaticCredentials(accessKey2, secretKey2, "")
_, err = creds.Get()
if err != nil {
logrus.Errorf("bad credentials: %s", err)
}
cfg = aws.NewConfig().WithEndpoint(endpointStr).WithDisableSSL(true).WithCredentials(creds).WithS3ForcePathStyle(true)
if len(regionStr) > 0 {
cfg = cfg.WithRegion(regionStr)
} else {
cfg = cfg.WithRegion(DefaultRegion)
}
svc = s3.New(session.New(), cfg)
objName = uuid.New().String()
fmt.Printf("--- PUT OBJECT %s IN BUCKET %s ---\n", objName, bucketNameTwo)
putObject(svc, bucketNameTwo, objName)
fmt.Printf("--- GET OBJECT %s IN BUCKET %s ---\n", objName, bucketNameTwo)
getObject(svc, bucketNameTwo, objName)
fmt.Printf("--- DONE ---\n")
fmt.Printf("\n\n")
time.Sleep(15 * time.Second)
}
}
func listBuckets(svc *s3.S3) {
result, err := svc.ListBuckets(nil)
if err != nil {
exitErrorf("Unable to list buckets, %v", err)
}
fmt.Println("Buckets:")
for _, b := range result.Buckets {
fmt.Printf("* %s \n", aws.StringValue(b.Name))
}
}
func createBucket(svc *s3.S3, bucketName string) {
input := &s3.CreateBucketInput{
Bucket: aws.String(bucketName),
}
result, err := svc.CreateBucket(input)
if handleError(err) {
return
}
fmt.Println("Bucket created ", result)
}
func deleteBucket(svc *s3.S3, bucketName string) {
input := &s3.DeleteBucketInput{
Bucket: aws.String(bucketName),
}
result, err := svc.DeleteBucket(input)
if handleError(err) {
return
}
fmt.Println("Bucket deleted ", result)
}
func putObject(svc *s3.S3, bucketName string, objName string) {
b := bytes.NewBufferString("This string is just for testing")
input := &s3.PutObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(objName),
Body: bytes.NewReader(b.Bytes()),
}
result, err := svc.PutObject(input)
if handleError(err) {
return
}
fmt.Println(result)
}
func getObject(svc *s3.S3, bucketName string, objName string) {
input := &s3.GetObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(objName),
}
result, err := svc.GetObject(input)
if handleError(err) {
return
}
fmt.Println(result)
}
func deleteObject(svc *s3.S3, bucketName string, objName string) {
input := &s3.DeleteObjectInput{
Bucket: aws.String(bucketName),
Key: aws.String(objName),
}
result, err := svc.DeleteObject(input)
if handleError(err) {
return
}
fmt.Println(result)
}
func handleError(err error) bool {
if err != nil {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case s3.ErrCodeNoSuchKey:
fmt.Println(s3.ErrCodeNoSuchKey, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
fmt.Println(err.Error())
}
return true
}
return false
}
func exitErrorf(msg string, args ...interface{}) {
fmt.Fprintf(os.Stderr, msg+"\n", args...)
os.Exit(1)
}