我在尝试使用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吗?我有一个简单的方法来处理这个问题吗?
答案 0 :(得分: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 {