2 回答

TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
根據(jù)此文檔,您可以處理您的自定義事件。因此您可以創(chuàng)建包含 S3Entity 和 SNSEntity 的自定義事件
type Record struct {
? ?EventVersion? ? ? ? ?string? ? ? ? ? ?`json:"EventVersion"`
? ?EventSubscriptionArn string? ? ? ? ? ?`json:"EventSubscriptionArn"`
? ?EventSource? ? ? ? ? string? ? ? ? ? ?`json:"EventSource"`
? ?SNS? ? ? ? ? ? ? ? ? events.SNSEntity `json:"Sns"`
? ?S3? ? ? ? ? ? ? ? ? ?events.S3Entity? `json:"s3"`
}
type Event struct {
? ? Records []Record `json:"Records"`
}
然后檢查事件源
func handler(event Event) error {
? ?if len(event.Records) > 0 {
? ? if event.Records[0].EventSource == "aws:sns" {
? ? ? ?//Do Something
? ? } else {
? ? ? ?//Do Something
? ? }
? }
? return nil
}

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超3個(gè)贊
你可以使用 Go 中的嵌入來(lái)解決這個(gè)問(wèn)題
import (
? ? "github.com/aws/aws-lambda-go/events"
? ? "github.com/aws/aws-lambda-go/lambda"
? ? "reflect"
)
type Event struct {
? ? events.SQSEvent
? ? events.APIGatewayProxyRequest
? ? //other event type
}
type Response struct {
? ? events.SQSEventResponse `json:",omitempty"`
? ? events.APIGatewayProxyResponse `json:",omitempty"`
? ?//other response type
}
func main() {
? ? lambda.Start(eventRouter)
}
func eventRouter(event Event) (Response, error) {
? ? var response Response
? ? switch {
? ? case reflect.DeepEqual(event.APIGatewayProxyRequest, events.APIGatewayProxyRequest{}):
? ? ? ? response.SQSEventResponse = sqsEventHandler(event.SQSEvent)
? ? case reflect.DeepEqual(event.SQSEvent, events.SQSEvent{}):
? ? ? ? response.APIGatewayProxyResponse = apiGatewayEventHandler(event.APIGatewayProxyRequest)
? //another case for a event handler
? ? }
? ? return response, nil
}
func sqsEventHandler(sqsEvent events.SQSEvent) events.SQSEventResponse {
? ? //do something with the SQS event?
}
func apiGatewayEventHandler(apiEvent events.APIGatewayProxyRequest) events.APIGatewayProxyResponse {
? ? //do something with the API Gateway event
}
注意:如果基本事件有一些相同的字段名稱(chēng),您將需要尋找另一個(gè) DeepEqual 的比較方法實(shí)例。
- 2 回答
- 0 關(guān)注
- 196 瀏覽
添加回答
舉報(bào)