Gau*_*ana 8 go amazon-web-services amazon-dynamodb
我在dynamodb.
我想在其中插入我从 HTML 表单中获取的 2 个参数“标题”和“内容”。
参数“blogContent”和“BLOGTITLE”似乎当我在控制台打印这些2是有效的。
但是当我将它们插入表中时,出现错误:
“一个或多个参数值无效:缺少项目状态代码中的密钥 ID:400,请求 ID:71CT5IPM1SIDKSDVUGWGUCSJ77VV4KQNSO5ZAMVJF66Q9ASUAAJG”
type Item struct {
id int
content string
bodycontentversion string
claps int
comments int
imageid int
title string
views int
}
func awsblog() {
sess, err := session.NewSession(&aws.Config{
Region: aws.String("us-east-1")},
)
svc := dynamodb.New(sess)
item := Item{
id: 1234,
content: blogContent,
bodycontentversion: "abcd",
claps: 5,
comments: 10,
imageid: 1234,
title: blogTitle,
views: 10,
}
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
fmt.Println("Got error marshalling new item:")
fmt.Println(err.Error())
os.Exit(1)
}
tableName := "Blog"
input := &dynamodb.PutItemInput{
Item: av,
TableName: aws.String(tableName),
}
_, err = svc.PutItem(input)
if err != nil {
fmt.Println("Got error calling PutItem:")
fmt.Println(err.Error())
os.Exit(1)
}
fmt.Println("Successfully updated '")
}
Run Code Online (Sandbox Code Playgroud)
结构域必须是公开的,才能MarshalMap发挥它的魔力。
添加一个fmt.Printf("marshalled struct: %+v", av)以查看该函数的输出。
这将反过来要求 DynamoDB 字段大写,除非您json:"field-name"向结构字段添加指令。
结果将是:
type Item struct {
Id int `json:"id"`
Content string `json:"content"`
Bodycontentversion string `json:"bodycontentversion"`
Claps int `json:"claps"`
Comments int `json:"comments"`
Imageid int `json:"imageid"`
Title string `json:"title"`
Views int `json:"views"`
}
Run Code Online (Sandbox Code Playgroud)
当我将一个项目与 boto3 Python 包一起放置时,我遇到了同样的错误。
ClientError: An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Missing the key id in the item
Run Code Online (Sandbox Code Playgroud)
问题是我在插入时没有正确指定分区键。我在下面错误地指定了 PARTITION_KEY。我拼错了变量名称(下面第一行),这导致了错误,但从错误消息中无法立即看到该错误。
PARTITION_KEY = 'hello'
SORT_KEY = 'world'
dynamodb = boto3.client('dynamodb')
dynamodb.put_item(
TableName=TABLE,
Item={
PARTITION_KEY: {'S': 'my-part-key'},
SORT_KEY: {'S': 'my-sort-key'},
}
)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
14662 次 |
| 最近记录: |