drex88669 2018-02-28 19:10
浏览 90
已采纳

使用go在dynamodb中创建项目

I'm using the follow code to create a item in my dynamodb table:

package main

import (
    "fmt"

    "github.com/aws/aws-sdk-go-v2/aws"
    "github.com/aws/aws-sdk-go-v2/aws/endpoints"
    "github.com/aws/aws-sdk-go-v2/aws/external"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb"
    "github.com/aws/aws-sdk-go-v2/service/dynamodb/dynamodbattribute"
)

type Record struct {
    ID     string
    URLs   []string
}


// Using the SDK's default configuration, loading additional config
// and credentials values from the environment variables, shared
// credentials, and shared configuration files
var cfg, err = external.LoadDefaultAWSConfig()

func createNewExtraction() error {
    svc := dynamodb.New(cfg)

    r := Record{
        ID:   "ABC123",
        URLs: []string{
            "https://example.com/first/link",
            "https://example.com/second/url",
        },
    }
    item, err := dynamodbattribute.MarshalMap(r)
    if err != nil {
        panic(fmt.Sprintf("failed to DynamoDB marshal Record, %v", err))
    }

    req := svc.PutItemRequest(&dynamodb.PutItemInput{
        TableName: aws.String("test"), Item: item })

    _, err = req.Send(); if err != nil {
        return err
    }
    return nil
}

func main() {
    if len(cfg.Region) > 0 {
        // Set Region to us-east-1 as default.
        cfg.Region = endpoints.UsEast1RegionID
    }

    err = createNewExtraction(); if err != nil {
        panic(err.Error())
    }
}

But it's returning the error:

panic: ValidationException: One or more parameter values were invalid: Missing the key id in the item
    status code: 400, request id: F3VCQSGRIG5GM5PEJE7T5M4CEVVV4KQNSO5AEMVJF66Q9ASUAAJG

goroutine 1 [running]:
main.main()
    /Users/user/go/src/Test2/test.go:56 +0x102
exit status 2

I already tried to declare Id, id and ID in my Record struct, but it doesn't work.

The stranger is: I got this code in the official documentation (I'm updating to work with the aws-sdk-go-v2).

Thank you in advance.

  • 写回答

1条回答 默认 最新

  • dongmang3961 2018-03-01 00:41
    关注

    I do not know golang, but I had similar problems in nodejs.

    Make sure the item you put in the table contains the 'partition key' and the sorting key, case-sensitive.

    EDIT:

    • It is a golang issue, the item is not built properly when the DynamoDB column names are lowercase.

    • Consider redefining the Record structure (see this link for details):

    type Record struct{
        ID     string `json:"id"`
        URLs   []string `json:"url"`
    }
    

    where id and url are column names in DynamoDB table.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程