I have made a table Blog
with DynamoDB.
I want to insert the 2 parameters title
and content
in it which I am fetching from an HTML form.
The parameters blogContent
and blogTitle
seem to be valid when I print these 2 in the console.
But when I am inserting them in the table i get the error:
One or more parameter values were invalid: Missing the key id in the item
status code: 400, request id: XXX"
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 '")
}
Struct fields must be public in order for MarshalMap
to do its magic.
Add a fmt.Printf("marshalled struct: %+v", av)
to see the output of that function.
That will in turn require the DynamoDB fields to be capitalised, unless you add a json:"field-name"
directive to the struct fields.
The result would be:
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"`
}
I had the same error when putting an item with the boto3 Python package.
ClientError: An error occurred (ValidationException) when calling the PutItem operation: One or more parameter values were invalid: Missing the key id in the item
The problem was that I did not specify the partition key correctly when inserting. I incorrectly specified PARTITION_KEY below. I misspelled the variable name (first line below), which caused the error which was not immediately visible from the error message.
PARTTION_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'},
}
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With