在 GO Lambda 中处理 DynamoDB 项目

Processing DynamoDB Items in GO Lambda

我正在执行简单的 table 扫描以从我的 DynamoDB table、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]

如何将这些项目转换为我在上面定义的结构,然后 return lambda 预期的 json 响应(基本上是一个 HTTP 响应代码和 [=19 中的项目=]表格)?

正在扫描 table returns 项的映射,并且您想将映射转换为结构,因此为了将映射列表转换为您要使用的结构 attributevalue.UnmarshalListOfMaps under aws-sdk-go-v2. In the previous version it was in the dynamodbattribute 但他们决定更改 v2 中的包。

products := []Product{}
err = attributevalue.UnmarshalListOfMaps(out.Items, &products)
if err != nil {
    panic(fmt.Sprintf("failed to unmarshal Dynamodb Scan Items, %v", err))
}

productsJson, err := json.Marshal(products)
if err != nil {
    panic(err)
}

我在 Product 结构中看到的一个问题是您需要使用导出的名称,还需要为结构字段定义 json 标记,否则数据将不会解组到结构中。因此,为了 return 来自 table 的扫描项目进入 json 响应,您的代码应该如下所示。


package main

import (
    "context"
    "encoding/json"
    "fmt"

    "github.com/aws/aws-lambda-go/events"
    "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/feature/dynamodb/attributevalue"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb"
)

type Product struct {
    ProductUUID string `json:"productUUID"`
    Name        string `json:"name"`
    Description string `json:"description"`
    Brand       string `json:"brand"`
    Price       string `json:"price"`
    Category    string `json:"category"`
    DateAdded   string `json:"dateAdded"`
}

func handleRequest() (events.APIGatewayProxyResponse, error) {
    products := []Product{}
    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)
    }

    err = attributevalue.UnmarshalListOfMaps(out.Items, &products)
    if err != nil {
        panic(fmt.Sprintf("failed to unmarshal Dynamodb Scan Items, %v", err))
    }

    productsJson, err := json.Marshal(products)
    if err != nil {
        panic(err)
    }

    resp := events.APIGatewayProxyResponse{
        StatusCode:      200,
        IsBase64Encoded: false,
        Body:            string(productsJson),
        Headers: map[string]string{
            "Content-Type": "application/json",
        },
    }

    return resp, nil
}

func main() {
    lambda.Start(handleRequest)
}

P.S: 扫描整个发电机 table 并将其作为响应 return 是非常昂贵的,你应该避免它。