Example of update_item in dynamodb boto3

后端 未结 5 773
陌清茗
陌清茗 2020-12-14 00:10

Following the documentation, I\'m trying to create an update statement that will update or add if not exists only one attribute in a dynamodb table.

I\'m trying this

5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-14 00:52

    Details on dynamodb updates using boto3 seem incredibly sparse online, so I'm hoping these alternative solutions are useful.

    get / put

    import boto3
    
    table = boto3.resource('dynamodb').Table('my_table')
    
    # get item
    response = table.get_item(Key={'pkey': 'asdf12345'})
    item = response['Item']
    
    # update
    item['status'] = 'complete'
    
    # put (idempotent)
    table.put_item(Item=item)
    

    actual update

    import boto3
    
    table = boto3.resource('dynamodb').Table('my_table')
    
    table.update_item(
        Key={'pkey': 'asdf12345'},
        AttributeUpdates={
            'status': 'complete',
        },
    )
    

提交回复
热议问题