1 回答

TA貢獻1909條經驗 獲得超7個贊
我會提出以下建議(我們在很多Go Lambdas中成功使用)。
main.go
[...]
func (h *handler) handleRequest(ctx context.Context) error {
input := h.s3Client.ListObjectsV2Input{
Bucket: aws.String("examplebucket"),
}
[...]
}
type handler struct {
s3Client s3iface.S3API
}
// main is called only once, when the Lambda is initialised (started for the first time). Code in this function should
// primarily be used to create service clients, read environments variables, read configuration from disk etc.
func main() {
h := handler{
s3client: s3.New(session.New()),
}
lambda.Start(h.handleRequest)
}
main_test.go
type ListObjectsV2Mock struct {
s3iface.S3API
output *s3.ListObjectsV2Output
}
func TestHandleRequest(t *testing.T) {
h := handler{
s3Client: &ListObjectsV2Mock{
output: &s3.ListObjectsV2Output{...},
},
}
err := h.HandleRequest(context.TODO())
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
}
顯然,缺少很多代碼(導入,錯誤處理等),但這是它的要點。
- 1 回答
- 0 關注
- 131 瀏覽
添加回答
舉報