forked from fujiwara/Rin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathevent.go
65 lines (56 loc) · 1.36 KB
/
event.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
package rin
import (
"encoding/json"
"fmt"
"net/url"
"strings"
)
func ParseEvent(b []byte) (Event, error) {
var e Event
err := json.Unmarshal(b, &e)
for _, r := range e.Records {
if !strings.Contains(r.S3.Object.Key, "%") {
continue
}
if _key, err := url.QueryUnescape(r.S3.Object.Key); err == nil {
r.S3.Object.Key = _key
}
}
return e, err
}
type Event struct {
Records []*EventRecord `json:"Records"`
}
func (e Event) String() string {
s := make([]string, len(e.Records))
for i, r := range e.Records {
s[i] = r.String()
}
return strings.Join(s, ", ")
}
type EventRecord struct {
EventVersion string `json:"eventVersion"`
EventName string `json:"eventName"`
EventSource string `json:"eventSource"`
EventTime string `json:"eventTime"`
AWSRegion string `json:"awsRegion"`
S3 S3Event `json:"s3"`
}
func (r EventRecord) String() string {
return r.EventName + " " + fmt.Sprintf(S3URITemplate, r.S3.Bucket.Name, r.S3.Object.Key)
}
type S3Event struct {
S3SchemaVersion string `json:"s3SchemaVersion"`
ConfigurationID string `json:"configurationId"`
Bucket S3Bucket `json:"bucket"`
Object S3Object `json:"object"`
}
type S3Bucket struct {
Name string `json:"name"`
ARN string `json:"arn"`
}
type S3Object struct {
Key string `json:"key"`
Size int64 `json:"size"`
ETag string `json:"eTag"`
}