亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 Go 中創建 AWS Lambda 來處理多個事件

如何在 Go 中創建 AWS Lambda 來處理多個事件

Go
蕭十郎 2023-06-26 15:47:31
我需要實施 AWS Lambda 處理程序來處理 AWS S3Events 和 SNSEvent,有什么解決方案嗎?How to support more one trigger in AWS Lambda in Golang?但這對我不起作用。
查看完整描述

2 回答

?
瀟湘沐

TA貢獻1816條經驗 獲得超6個贊

根據此文檔,您可以處理您的自定義事件。因此您可以創建包含 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

}


查看完整回答
反對 回復 2023-06-26
?
慕森王

TA貢獻1777條經驗 獲得超3個贊

你可以使用 Go 中的嵌入來解決這個問題

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

}

注意:如果基本事件有一些相同的字段名稱,您將需要尋找另一個 DeepEqual 的比較方法實例。


查看完整回答
反對 回復 2023-06-26
  • 2 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號