登录
首页 >  Golang >  Go问答

AWS PutItem 未在数据库中创建条目

来源:stackoverflow

时间:2024-04-12 17:36:33 473浏览 收藏

来到golang学习网的大家,相信都是编程学习爱好者,希望在这里学习Golang相关编程知识。下面本篇文章就来带大家聊聊《AWS PutItem 未在数据库中创建条目》,介绍一下,希望对大家的知识积累有所帮助,助力实战开发!

问题内容

我正在尝试编写一个 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 表中看不到任何项目。它是空的。我不知道在哪里寻找问题的根源。同时我可以看到有写入表。

我已经按照 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}

正确答案


您尚未证明表中没有任何项目。您是否看到“项目计数”为 0 并认为这是及时的? “项目摘要”标题下的通知表示计数仅每 6 小时更新一次。尝试点击“获取实时项目计数”按钮,我打赌您会看到一些项目。

为什么项目计数没有持续维护?因为它是一个大型分布式数据库,可以存储数万亿个项目。获取实时项目计数并不总是一个轻量级的过程。

以上就是《AWS PutItem 未在数据库中创建条目》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>