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
|
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/s3"
)
// Feed represents feed.json structure
type Feed struct {
Podcast struct {
LastBuildDate string `json:"lastBuildDate"`
Title string `json:"title"`
Description string `json:"description"`
Image struct {
URL string `json:"url"`
Title string `json:"title"`
Link string `json:"link"`
} `json:"image"`
Link string `json:"link"`
Author string `json:"author"`
Copyright string `json:"copyright"`
Email string `json:"email"`
Language string `json:"language"`
Type string `json:"type"`
CategoryMain string `json:"category_main"`
CategorySub string `json:"category_sub"`
Explicit string `json:"explicit"`
Episodes []struct {
Title string `json:"title"`
Description string `json:"description"`
Link string `json:"link"`
Audio string `json:"audio"`
Creator string `json:"creator"`
Explicit string `json:"explicit"`
Duration string `json:"duration"`
Image string `json:"image"`
Episode int `json:"episode"`
Type string `json:"type"`
} `json:"episodes"`
} `json:"podcast"`
}
// GOOS=linux GOARCH=arm64 CGO_ENABLED=0 go build -o bootstrap -tags lambda.norpc main.go
// zip myFunction.zip bootstrap
func main() {
lambda.Start(handler)
}
func handler(ctx context.Context, s3Event events.S3Event) error {
sess := session.Must(session.NewSession())
svc := s3.New(sess)
object := s3Event.Records[0].S3.Object
bucket := "intoxicating"
key := object.Key
// Download object from S3
resp, err := svc.GetObject(&s3.GetObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
})
if err != nil {
fmt.Println("Error while downloading the object", err)
return err
}
defer resp.Body.Close()
// Decode JSON payload into struct
var feed Feed
json.NewDecoder(resp.Body).Decode(&feed)
// Generate RSS feed based on feed.json
fmt.Println(feed)
rssFeed := generateRSS(feed)
fmt.Println("RSS feed input:")
// Upload RSS feed to S3 root path as feed.rss
var body bytes.Buffer
body.Write(rssFeed)
fmt.Println("RSS feed output:")
fmt.Println(string(rssFeed))
uploadResp, err := svc.PutObject(&s3.PutObjectInput{
Body: bytes.NewReader(body.Bytes()),
Bucket: aws.String(bucket),
Key: aws.String("feed.rss"),
ContentType: aws.String("application/xml"),
})
if err != nil {
fmt.Println("Error while uploading the object", err)
return err
}
fmt.Printf("Feed uploaded to S3 with status %v\n", uploadResp)
return nil
}
|