0

我在尝试使用 godynamo 管理 dynamodb 实例时遇到了问题。

我的代码旨在采用 gob 编码的字节数组并将其放入 dynamodb。

func (c *checkPointManager) CommitGraph(pop *Population) {
    var blob, err = pop.GobEncodeColorGraphs()
    fitness := pop.GetTotalFitness()
    if err != nil {
            log.Fatal(err)
    }

    put1 := put.NewPutItem()
    put1.TableName = "CheckPoint"
    put1.Item["fitnessScore"] = &attributevalue.AttributeValue{N: string(fitness)}
    put1.Item["population"] = &attributevalue.AttributeValue{N: string(1)}
    put1.Item["graph"] = &attributevalue.AttributeValue{B: string(blob)}
    body, code, err := put1.EndpointReq()
    if err != nil || code != http.StatusOK {
            log.Fatalf("put failed %d %v %s\n", code, err, body)
    }
    fmt.Printf("values checkpointed:  %d\n %v\n %s\n", code, err, body)

}

但是,每次运行此代码时,都会出现以下错误。无法转换为 Blob:Base64 编码长度应为 4 字节的倍数,但发现:25

Godynamo 不处理确保二进制数组专门转换为 base64 的问题吗?我有一个简单的方法来处理这个问题吗?

4

1 回答 1

0

根据Amazon DynamoDB 数据类型的二进制数据类型描述,“客户端应用程序必须以 base64 格式编码二进制值” 。

如果需要,您的代码可以对值进行编码,请参阅 golang 的 base64 包: https ://golang.org/pkg/encoding/base64

godynamo 库提供了可以为你编码的函数,看看AttributeValue

    // InsertB_unencoded adds a new plain string to the B field.
    // The argument is assumed to be plaintext and will be base64 encoded.
    func (a *AttributeValue) InsertB_unencoded(k string) error {
于 2015-05-28T06:04:31.063 回答