-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
68 lines (57 loc) · 1.59 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
package main
import (
"bytes"
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3/s3manager"
"github.com/aws/aws-sdk-go/service/sqs"
"os"
)
func main() {
// comment this out if you want to work on a real AWS account
err := os.Setenv("LOCALSTACK_ENDPOINT", "http://localhost:4566")
if err != nil {
panic(err)
}
// when running locally, we're going to have the LOCALSTACK_ENDPOINT environment variable set
sess, err := CreateSession("eu-central-1")
if err != nil {
panic(err)
}
sqsSvc := sqs.New(sess)
s3Svc := s3manager.NewUploader(sess)
for {
result, err := sqsSvc.ReceiveMessage(
&sqs.ReceiveMessageInput{
QueueUrl: aws.String("http://localhost:4566/000000000000/queue1"),
MaxNumberOfMessages: aws.Int64(1),
WaitTimeSeconds: aws.Int64(3),
},
)
if err != nil {
fmt.Printf("failed to receive message with error %v", err)
continue
}
if len(result.Messages) == 0 {
continue
}
_, err = s3Svc.Upload(&s3manager.UploadInput{Bucket: aws.String("bucket1"),
Key: aws.String("test1"),
Body: bytes.NewReader([]byte(*result.Messages[0].Body))},
)
if err != nil {
fmt.Printf("failed to upload key with error %v", err)
}
}
}
func CreateSession(region string) (*session.Session, error) {
awsConfig := &aws.Config{
Region: aws.String(region),
}
if localStackEndpoint := os.Getenv("LOCALSTACK_ENDPOINT"); localStackEndpoint != "" {
awsConfig.S3ForcePathStyle = aws.Bool(true)
awsConfig.Endpoint = aws.String(localStackEndpoint)
}
return session.NewSession(awsConfig)
}