AWS PutItem 不在数据库中创建条目
AWS PutItem not creating entries in DB
我正在尝试编写一个 lambda 函数,该函数将一个字符串数组写入一个通用集合,其中主键是集合的类型。它应该始终用新项目替换当前项目(如果存在,否则创建一个)。
这是我的处理函数
func Handler(ctx context.Context) (Response, error) {
item := lib.SkillsCollection{
Collection: lib.SkillsCollectionName,
Value: lib.Skills{
"test",
"test 2",
},
}
log.Printf("Prepare item %v", item)
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := dynamodb.New(sess)
log.Printf("Created session")
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
log.Fatalf("Marshall error mapping value %s", err)
}
log.Printf("Marshalled item %v", av)
input := &dynamodb.PutItemInput{
TableName: aws.String(lib.DbCollectionsTable),
Item: av,
}
log.Println("Start scanning collections")
_, err = svc.PutItem(input)
if err != nil {
log.Fatalf("Error during put %v", err)
}
var buf bytes.Buffer
body, err := json.Marshal(map[string]interface{}{
"message": "Go Serverless v1.0! Your function executed successfully!",
})
if err != nil {
return Response{StatusCode: 404}, err
}
json.HTMLEscape(&buf, body)
resp := Response{
StatusCode: 200,
IsBase64Encoded: false,
Body: buf.String(),
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return resp, nil
}
根据日志,一切正常。没有抛出任何错误,我得到状态为 200
的响应
这些是日志
但是,之后我在 DynamoDB 中看不到任何项目 table。它是空的。我不知道在哪里寻找问题的根源。同时我可以看到有写入 table.
我已经按照 AWS 文档中的示例重写了解决方案,但没有成功。
万一重要的结构
type Skills []string
type SkillsCollection struct {
Collection string `dynamodbav:"collection" json:"collection"`
Value Skills `dynamodbav:"value" json:"value"`
}
和数据库设置配置
CollectionsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: collection
AttributeType: S
KeySchema:
- AttributeName: collection
KeyType: HASH
BillingMode: PAY_PER_REQUEST
TableName: ${self:custom.collectionsTableName}
您还没有证明 table 中没有项目。您是否看到“项目计数”为 0 并认为它是及时的?项目摘要 header 下的通知表示计数仅每 6 小时更新一次。尝试点击“获取实时项目数”按钮,我敢打赌您会看到一些项目。
为什么不能持续维护项目计数?因为它是一个大型分布式数据库,可以存储数万亿项。获取实时项目计数并不总是一个轻量级的过程。
我正在尝试编写一个 lambda 函数,该函数将一个字符串数组写入一个通用集合,其中主键是集合的类型。它应该始终用新项目替换当前项目(如果存在,否则创建一个)。
这是我的处理函数
func Handler(ctx context.Context) (Response, error) {
item := lib.SkillsCollection{
Collection: lib.SkillsCollectionName,
Value: lib.Skills{
"test",
"test 2",
},
}
log.Printf("Prepare item %v", item)
sess := session.Must(session.NewSessionWithOptions(session.Options{
SharedConfigState: session.SharedConfigEnable,
}))
svc := dynamodb.New(sess)
log.Printf("Created session")
av, err := dynamodbattribute.MarshalMap(item)
if err != nil {
log.Fatalf("Marshall error mapping value %s", err)
}
log.Printf("Marshalled item %v", av)
input := &dynamodb.PutItemInput{
TableName: aws.String(lib.DbCollectionsTable),
Item: av,
}
log.Println("Start scanning collections")
_, err = svc.PutItem(input)
if err != nil {
log.Fatalf("Error during put %v", err)
}
var buf bytes.Buffer
body, err := json.Marshal(map[string]interface{}{
"message": "Go Serverless v1.0! Your function executed successfully!",
})
if err != nil {
return Response{StatusCode: 404}, err
}
json.HTMLEscape(&buf, body)
resp := Response{
StatusCode: 200,
IsBase64Encoded: false,
Body: buf.String(),
Headers: map[string]string{
"Content-Type": "application/json",
},
}
return resp, nil
}
根据日志,一切正常。没有抛出任何错误,我得到状态为 200
的响应这些是日志
但是,之后我在 DynamoDB 中看不到任何项目 table。它是空的。我不知道在哪里寻找问题的根源。同时我可以看到有写入 table.
我已经按照 AWS 文档中的示例重写了解决方案,但没有成功。
万一重要的结构
type Skills []string
type SkillsCollection struct {
Collection string `dynamodbav:"collection" json:"collection"`
Value Skills `dynamodbav:"value" json:"value"`
}
和数据库设置配置
CollectionsTable:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: collection
AttributeType: S
KeySchema:
- AttributeName: collection
KeyType: HASH
BillingMode: PAY_PER_REQUEST
TableName: ${self:custom.collectionsTableName}
您还没有证明 table 中没有项目。您是否看到“项目计数”为 0 并认为它是及时的?项目摘要 header 下的通知表示计数仅每 6 小时更新一次。尝试点击“获取实时项目数”按钮,我敢打赌您会看到一些项目。
为什么不能持续维护项目计数?因为它是一个大型分布式数据库,可以存储数万亿项。获取实时项目计数并不总是一个轻量级的过程。