我正在执行一个简单的表扫描以从我的 DynamoDB 表中获取所有项目,products. 这是整个 Lambda:
package main
import (
"context"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
)
type Product struct {
productUUID string
name string
description string
brand string
price string
category string
dateAdded string
}
func handleRequest() (string, error) {
cfg, err := config.LoadDefaultConfig(context.TODO(), func(o *config.LoadOptions) error {
o.Region = "us-east-2"
return nil
})
if err != nil {
panic(err)
}
svc := dynamodb.NewFromConfig(cfg)
out, err := svc.Scan(context.TODO(), &dynamodb.ScanInput{
TableName: aws.String("products"),
})
if err != nil {
panic(err)
}
for _, item := range out.Items {
fmt.Println(item)
}
return "Items", nil // Temporary debugging return (always 200). I'd like to return a json object here
}
func main() {
lambda.Start(handleRequest)
}
当我取回数据时,我可以按以下形式打印出每一项:
map[brand:0xc0002f38a8 category:0xc0002f3848 dateAdded:0xc0002f3830 name:0xc0002f3800 price:0xc0002f37a0 productUUID:0xc0002f3818]
如何将这些项目转换为我在上面定义的结构,然后返回 lambda 预期的 json 响应(基本上是 HTTP 响应代码和 json 形式的项目)?
慕码人2483693
相关分类