【问题标题】:What method should I use in Boto3 to create an entry in DynamoDB using AWS API gateway as a proxy service?我应该在 Boto3 中使用什么方法来使用 AWS API 网关作为代理服务在 DynamoDB 中创建条目?
【发布时间】:2021-06-23 12:03:50
【问题描述】:

我是编码新手。我很感激帮助。 我已经创建了一个 DynamoDB 表、API 网关、子资源、子方法(POST)、集成类型:AWS 服务代理。我还创建了一个 IAM 角色以允许访问 DynamoDB putitem。 我试图用邮递员在桌子上添加一些东西,效果很好。但是,现在我想用 boto3 向表中添加一个项目,但我发现很难确定我应该使用的方法。

【问题讨论】:

    标签: amazon-dynamodb aws-api-gateway boto3


    【解决方案1】:
    dydbsr = boto3.resource('dynamodb')
    dbtable = 'myTable'
    table = dydbsr.Table(dbtable)
    

    如果您只有一个分区键:

    partion key is: mypartitionkey
    
    json_dictionary = {"mypartitionkey": "myvalue"}
    table.put_item(Item=json_dictionary)
    

    如果您有分区键和排序键。如果定义了排序键,则必须包含

    partion key is: mypartitionkey
    sort key is: mysortkey
    
    json_dictionary = {"mypartitionkey": "myvalue", "mysortkey": "myothervalue"}
    table.put_item(Item=json_dictionary)
    

    添加分区键和排序键后,你的属性就是同一个json字典中的key:value对

    【讨论】:

    • 感谢乔纳森的彻底回复。
    【解决方案2】:

    有关使用 Python 创建、读取、更新和删除项目的示例,请参阅 AWS 文档 here

    【讨论】:

    • 谢谢你。感谢您的回复。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-03-10
    • 2017-01-22
    • 2011-11-11
    • 2019-08-28
    • 2017-06-22
    • 2011-10-14
    • 2017-11-06
    相关资源
    最近更新 更多