简体   繁体   English

如何将 DynamoDB LastEvaluatedKey 序列化为字符串

[英]How to serialize DynamoDB LastEvaluatedKey to string

Have a requirement of transferring the LastEvaluatedKey returned from the query output as the response of a paginated API call so that the users can request the next page with the LastEvaluatedKey.要求将查询 output 返回的 LastEvaluatedKey 传递为分页 API 调用的响应,以便用户可以使用 LastEvaluatedKey 请求下一页。 Is it possible to convert with aws-sdk-go-v2?是否可以使用 aws-sdk-go-v2 进行转换?

Have tried to marshal and unmarshal using json but it was not working已尝试使用 json 编组和解组,但它不起作用

lek := map[string]types.AttributeValue{
    "num":  &types.AttributeValueMemberN{Value: "1"},
    "text": &types.AttributeValueMemberS{Value: "text"},
}
barray, err := json.Marshal(lek)
if err != nil {
  fmt.println(err)
}

lekDecoded := map[string]types.AttributeValue{}
err = json.Unmarshal(barray, &lekDecoded)
if err != nil {
 fmt.println(err)
}

This always keeps failing to decode it back to map[string]types.AttributeValue这总是无法将其解码回 map[string]types.AttributeValue

LastEvaluatedKey is provided in plain text in the response. LastEvaluatedKey在响应中以纯文本形式提供。 If you want to encode it, you can do so using base64 for example.如果你想对其进行编码,你可以使用 base64 来编码。 You can choose any type of encoding so long as you decode before supplying back to the next paged request.您可以选择任何类型的编码,只要您在提供回下一个分页请求之前进行解码即可。

Encode LEK编码 LEK

aws dynamodb scan \
--table-name test \
--limit 1 \
--query LastEvaluatedKey | base64
ewogICAgImlkIjogewogICAgICAgICJTIjogIjR3OG5pWnBpTXk2cXoxbW50RkE1dSIKICAgIH0KfQo

Decode encoded LEK解码编码的 LEK

echo ewogICAgImlkIjogewogICAgICAgICJTIjogIjR3OG5pWnBpTXk2cXoxbW50RkE1dSIKICAgIH0KfQo | base64 --decode
{
    "id": {
        "S": "4w8niZpiMy6qz1mntFA5u"
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM