2015-05-27 27 views
0

我遇到了试图用godynamo管理dynamodb实例的问题。将二进制数据发送给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吗?有没有简单的方法来处理这个问题?

回答

0

根据Amazon DynamoDB Data Types的二进制数据类型说明,“客户端应用程序必须以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 { 
相关问题